English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include


void main()
{
int i;
short r[6] = { 10,4567,4321,30897,5467,2};
float p[6] = {0,0,0,0,0,0};

for(i=0;i<6;i++)
{
p[i] = (float) (r[i]/32768);
printf("%.9lf\n", p[i]);
}
}

2007-03-10 10:42:36 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

It looks like you're trying to multiply (float) which has no definition or value times (r[i]/32768)

2007-03-10 11:10:56 · answer #1 · answered by InjunRAIV 6 · 0 0

You are dividing an integer by an integer which always yields an integer. For fractions the result is truncated to zero.

The numerator needs to be cast as a float and then divide.

For example:

#include

using namespace std;

int main( int argc, char* argv[] )
{
int small = 10;
int big = 30;

int resultInt = small/big;
float resultFloatWrong = small/big;
float resultFloatRight = ((float) small)/big;

cout << "resultInt: " << resultInt << endl;
cout << "resultFloatWrong: " << resultFloatWrong << endl;
cout << "resultFloatRight: " << resultFloatRight << endl;
}

Yielded the following:

[721] answers: g++ -Wall divide.cpp
[722] answers: ./a.out
resultInt: 0
resultFloatWrong: 0
resultFloatRight: 0.333333
[723] answers:

2007-03-11 03:33:37 · answer #2 · answered by Dan M 1 · 0 0

fedest.com, questions and answers