473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2940
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
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...
4
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...
16
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
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...
1
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...
7
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...
15
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...
10
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
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...
39
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.