Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Floating point problem - Linux vs Solaris

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
I an doing a computation where in a double value is multiplied with a long long.

For ex:
double a;
long long b;
long long res;

..
..
...
res = a * b;
.....
....
..

res is different on Solaris and Linux platforms

Ex: if a has a value of 8.66666666666666607455 and b = 2370
Linux gives me a value of 20540 but Solaris gives 20539.

Any idea on resolving folating point issues?
Any standard functions or algorithms or methodologies used in the industry??
 

Despite all your digits, your double has only 15 decimal digits of precision, so the last 6 or so digits are really rather meaningless. If you printed this out, then these are just noise.

Second point, which the URL will explain in detail, is that floats are approximations. So any results are also going to be approximations as well.

If your 8.6666 is really the result of 26/3, then you need to rearrange your calculations to be more computationally accurate.
[tt]26 / 3 * 2370[/tt] may be well defined mathematically, but computationally, you have rounding, underflow and overflow to contend with.
[tt]26 * 2370 / 3[/tt] produces the correct answer, and does not even need to get messy with floating point at all.

--
 
8.6666666... was just an example. It can be any floating point number.
In my case, this number is computed in a for loop.

double tmp, num1;
num1 = chain[j-1];
for (kk = (j-2); kk>=0; kk--)
{
tmp = (1.0 / num1);
num1 = tmp + chain[kk] ;
}
a=num1;
 
It seems, that one system rounds the result before converting to integer, where other simply cuts the rational part. Try to round your result before assigning:

res = a * b + 0.5;
 
> It seems, that one system rounds the result before converting to integer, where other simply cuts the rational part.

I'm wrong. The reason is, that results on different systems are 20539.9999999999 and 20540.0000000000 - difference of 0.0000000001 is fully acceptable in point of view of floating point approximation. Rational parts are simply truncated by converting to integer - so rounding is necessary.
 
Hi,
Are you running Both LINUX and Solaris on the Same CHIP SET?

Solaris INTEL vs LINUX INTEL
Solaris Sparc vs Linux Sparc ( if such a thing exists )?

If you are trying to compare Solaris SPARC against LINUX INTEL, then you probably aren't seeing issues related to the Software but running into limitations of the Hardware.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top