473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating Point Accuracy

Hi,

I have just tried compiling some scientific calculation code on Redhad
Linux using gcc-3.4.4 that I normally compile on MS Visual C++ 03 and
05. I was surprised to get different floating point answers on the two
systems (identical hardware spec AMD Athlon). Both are being compiled in
'Debug' mode with things like Fast_Math disabled.

After a little debuging and lots of printf() statements, all my errors
are found to be cumulative floating point rounding errors.

Most data is read in as floats and then casted to doubles for internal
matrix calculations, before being casted back to floats for storage and
output.

Questions:

1) What is the best way to deal with floating point behavior differences
on different systems?
2) In a printf("%N.Nf\n ",myVal); style statement, what is the highest
number of decimal places that one can be accurate to? %8.3f / %9.4f /
%10.5f ???
3) There is a PRAGMA on VC to allow function level setting of
FloatingPoint compile mode so that you can set Fast/Precise for
individual functions. Is there a cross-platform way to do this?

Many thanks for any help,
There is quite a lot of hair loss over here ;-)
Jon Rea
Apr 6 '06 #1
8 2957
Jon Rea wrote:
I have just tried compiling some scientific calculation code on Redhad
Linux using gcc-3.4.4 that I normally compile on MS Visual C++ 03 and
05. I was surprised to get different floating point answers on the two
systems [...]

Questions:

1) What is the best way to deal with floating point behavior
differences on different systems? [...]


http://www.google.com/search?hl=en&q...floating+point

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '06 #2
"Jon Rea" writes:
1) What is the best way to deal with floating point behavior differences
on different systems?


As far as I know you just live with it. I don't think there is any way to
get each bit to agree on two different platforms. You write your code such
that you can live with the answers in the least precise form. You have
already been given a link on that.
Apr 6 '06 #3
osmium wrote:
"Jon Rea" writes:
1) What is the best way to deal with floating point behavior differences
on different systems?


As far as I know you just live with it. I don't think there is any way to
get each bit to agree on two different platforms. You write your code such
that you can live with the answers in the least precise form. You have
already been given a link on that.


Many thanks for your rapid replies, very interesting! Ive had a skim
read of that article, but I'll go back and have a full read later.

I originally thought that differences in floating point were derived
from differences in the way a given hardware architecture dealt with the
number, i.e. the special floating point parts of the processor core.
Still not sure I totally understand why there should therefore be any
differences between Linux and Windows on the same hardware... Ill have
to read more ;-)

Cheers,
Jon
Apr 6 '06 #4
Jon Rea wrote:

I originally thought that differences in floating point were derived
from differences in the way a given hardware architecture dealt with the
number, i.e. the special floating point parts of the processor core.
Still not sure I totally understand why there should therefore be any
differences between Linux and Windows on the same hardware... Ill have
to read more ;-)


If you add a small number to a big number, many times, they might
disappear. If you add together several small numbers and then add that
to the big number, then it might make a difference. Hence, the order of
addition of numbers becomes relevant if they are of significantly
differing magnitudes. By the time you do a few multiplications and
additions, you can be quite a way off target before you know it.

Compilers can decide to reorder the additions (or not) as they see fit,
so it's quite possible that a different compiler, or even compiler
version will produce differing results.

Performing calculations on floating point numbers is like moving piles
of sand, every time you do it, you lose a bit of sand and pick up a bit
of dirt.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 6 '06 #5
Jon Rea wrote:
Hi,

I have just tried compiling some scientific calculation code on Redhad
Linux using gcc-3.4.4 that I normally compile on MS Visual C++ 03 and
05. I was surprised to get different floating point answers on the two
systems (identical hardware spec AMD Athlon). Both are being compiled in
'Debug' mode with things like Fast_Math disabled.


Depends. Are you using float, double, or long double?

AFAIK, on MS and G++ float is IEEE 32 bit, and double is IEEE 64 bits.
You run into differences with long double, though. MS makes it 64 bits,
while g++ uses a (denormalized) 80 bit value.
Apr 6 '06 #6
red floyd wrote:
Jon Rea wrote:
Hi,

I have just tried compiling some scientific calculation code on Redhad
Linux using gcc-3.4.4 that I normally compile on MS Visual C++ 03 and
05. I was surprised to get different floating point answers on the two
systems (identical hardware spec AMD Athlon). Both are being compiled
in 'Debug' mode with things like Fast_Math disabled.


Depends. Are you using float, double, or long double?

AFAIK, on MS and G++ float is IEEE 32 bit, and double is IEEE 64 bits.
You run into differences with long double, though. MS makes it 64 bits,
while g++ uses a (denormalized) 80 bit value.


I'm just using floats and doubles. I'm getting differences between MS
and g++ even though they are both supposed to use the same 'IEEE 32 bit'
standard.
Apr 6 '06 #7
"Jon Rea" writes:
I'm just using floats and doubles. I'm getting differences between MS and
g++ even though they are both supposed to use the same 'IEEE 32 bit'
standard.


AFAIK there is no requirement that the output routines have to provide the
actual value represented in the variable. The days of printing 2.99999999
instead of 3 are over and my guess is that this was accomplished by a little
judicious juggling in the conversion to characters. If you are determined
to resolve this, you might look at the data in hex to see what's really
there.
Apr 6 '06 #8
Jon Rea wrote:
red floyd wrote:
Jon Rea wrote:
I have just tried compiling some scientific calculation code on
Redhad Linux using gcc-3.4.4 that I normally compile on MS Visual C++
03 and 05. I was surprised to get different floating point answers on
the two systems (identical hardware spec AMD Athlon). Both are being
compiled in 'Debug' mode with things like Fast_Math disabled.

Depends. Are you using float, double, or long double?
AFAIK, on MS and G++ float is IEEE 32 bit, and double is IEEE 64 bits.
You run into differences with long double, though. MS makes it 64
bits, while g++ uses a (denormalized) 80 bit value.

I'm just using floats and doubles. I'm getting differences between MS
and g++ even though they are both supposed to use the same 'IEEE 32 bit'
standard.


You can still get differences because some compilers generate code that
retains full 80 bit precision on intermediate values, whereas others
truncate intermediate values to 64 bits. Both are allowed by the
standard, but the latter implementation is a bad idea as far as I'm
concerned.

The Digital Mars C++ compiler fully supports 80 bit long doubles, and
also does all temporaries to full 80 bit precision. The CPU supports 80
bit precision, why not use it?

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Apr 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7830
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places =...
4
3291
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or...
16
6203
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
5
3729
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging...
1
3847
by: anagai | last post by:
hi Isnt it true that floating type can generate rounding errors if the values are rounded to like the 10th decimal. Since currency is rounded only to the 2nd decimal why must i worry about using float for currency calculations? decimal is slow and memory hog. is it true that mysql calculates internally using double float?
7
2571
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger than f(n) so that x0 + f(n) == x0. For example, x0 could be 2**300*(1.1234...) while f(n) is 2**100*(1.4256...). If x0 + f(n) == x0 I still sometimes...
15
3902
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that...
10
18741
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
18
2812
by: n.torrey.pines | last post by:
I understand that with floats, x*y == y*x, for example, might not hold. But what if it's the exact same operation on both sides? I tested this with GCC 3.4.4 (Cygwin), and it prints 0 (compiled with - g flag) #include <cmath> #include <iostream> int main() {
39
3526
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.