Thursday, January 9, 2014

different rounding behavior with float and double

Newsgroup: comp.lang.c++

Subject: different rounding behavior with float and double

From: Raymond Li <faihk1@...>

Date: Thu, 09 Jan 2014 17:46:51 +0800



I have encountered a problem related to floating point rounding. I

googled a lot and there are many clear and helpful information. e.g.



http://www.learncpp.com/cpp-tutorial/25-floating-point-numbers/

http://support.microsoft.com/kb/125056/en-hk





Although the urls have explained the cause, I need to find a practical

way to solve a rounding problem. My program has calculated a weighted

accumulation as 3.5. When the figure is rounded to nearest number, it

became 3 (but I want it to round up to 4). I understood it would be due

to approximation value of 3.5 as 3.49999...



I found a simple fix by using float instead of double. I list the

program below and wish someone could explain why using double would

incur the rounding problem while float would not. In the code below,

fun1() use float and the calculation is 'correct'. In fun2(), it uses

double and the figure 3.5 is rounded as 3.



Raymond







//######################









#include <cmath>

#include <iostream>



//using namespace std;



using std::cout;

using std::endl;

int fun1();

int fun2();



int main(int argc, char ** argv)

{

fun1();

fun2();

return 0;

}







int fun1()

{



float weighted=10.0;

float average=100.0;

float z[]=

{

4.0,

4.0,

4.0,

4.0,

4.0,

3.0,

3.0,

3.0,

2.0,

4.0

};



float total=0.0;



int i=0;

for (i=0;i<10;i++)

{

float item=z[i]*weighted/average;

total=total+item;

cout << i << " accumulate is " << total << endl;

// NSLog(@... is %f, total is %f", i, z[i], total);

}



float answer=round(total);

// NSLog(@... is %f", answer);

cout << "rounded is " << answer << endl;

return 0;

}





int fun2()

{



double weighted=10.0;

double average=100.0;

double z[]=

{

4.0,

4.0,

4.0,

4.0,

4.0,

3.0,

3.0,

3.0,

2.0,

4.0

};



double total=0.0;



int i=0;

for (i=0;i<10;i++)

{

double item=z[i]*weighted/average;

total=total+item;

cout << i << " accumulate is " << total << endl;

// NSLog(@... is %f, total is %f", i, z[i], total);

}



double answer=round(total);

// NSLog(@... is %f", answer);

cout << "rounded is " << answer << endl;

return 0;

}



0 accumulate is 0.4

1 accumulate is 0.8

2 accumulate is 1.2

3 accumulate is 1.6

4 accumulate is 2

5 accumulate is 2.3

6 accumulate is 2.6

7 accumulate is 2.9

8 accumulate is 3.1

9 accumulate is 3.5

rounded is 4

***(above is the version using float, 3.5 is rounded as 4) ***



0 accumulate is 0.4

1 accumulate is 0.8

2 accumulate is 1.2

3 accumulate is 1.6

4 accumulate is 2

5 accumulate is 2.3

6 accumulate is 2.6

7 accumulate is 2.9

8 accumulate is 3.1

9 accumulate is 3.5

rounded is 3



***(this version use double, 3.5 is rounded as 3) ***





--- news://freenews.netfront.net/ - complaints: news@... ---







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?167977-different-rounding-behavior-with-float-and-double&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment