473,382 Members | 1,749 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,382 software developers and data experts.

Float consistency

Hello,

float nanometers(long pm)
{
return pm / 1000.0f;
}

void f()
{
if(nanometers(309311L) == nanometers(309311L))
{
// do something
}
}

Is there any way to force the VS .NET 2003 C++ compiler to produce a result of true for the 'if'
expression above? I get false even with the /Op switch. The original code from which I created the
example produced true on an earlier compiler (1.52c). The project is large and I don't know how many
similar problems are lurking in it and there's no easy way to look for them.

DW
Sep 21 '06 #1
4 1174

"David W" <no@email.providedwrote in message
news:xj*******************@nasal.pacific.net.au...
Hello,

float nanometers(long pm)
{
return pm / 1000.0f;
}

void f()
{
if(nanometers(309311L) == nanometers(309311L))
{
// do something
}
}

Is there any way to force the VS .NET 2003 C++ compiler to produce a
result of true for the 'if'
expression above? I get false even with the /Op switch. The original code
from which I created the
example produced true on an earlier compiler (1.52c). The project is large
and I don't know how many
similar problems are lurking in it and there's no easy way to look for
them.

DW
The best advise is to upgrade to VS 2005 which doesn't exhibit this problem.
The VS 2003 C++ compiler is storing the intermediate float number in a dword
space in memory, but when it reloads it back into the floating point
register, there is extra garbage in the low order bits since the FP register
is much wider than the dword. VS 2005 C++ compiler gets around this by
storing the intermediate values in a qword memory space.

Having said this, this is still somewhat dangerous code to use. Checking for
equality between floating pointer numbers is frought with danger because of
implicit numerical roundoff when fractions are computed within the FP
registers and truncated to fit the register.

HTH

Brian
>

Sep 22 '06 #2
"Brian Muth" <bm***@mvps.orgwrote in message
news:#e**************@TK2MSFTNGP05.phx.gbl...
>
"David W" <no@email.providedwrote in message
news:xj*******************@nasal.pacific.net.au...
Hello,

float nanometers(long pm)
{
return pm / 1000.0f;
}

void f()
{
if(nanometers(309311L) == nanometers(309311L))
{
// do something
}
}

Is there any way to force the VS .NET 2003 C++ compiler to produce a
result of true for the 'if'
expression above? I get false even with the /Op switch. The original
code
from which I created the
example produced true on an earlier compiler (1.52c). The project is
large
and I don't know how many
similar problems are lurking in it and there's no easy way to look for
them.

DW

The best advise is to upgrade to VS 2005 which doesn't exhibit this
problem.

Quite an expensive fix, not to mention the time involved in getting a large
project rebuilt with a new compiler. No 2003 service pack that fixes it? If
there isn't there should be, given the outlay.
The VS 2003 C++ compiler is storing the intermediate float number in a
dword
space in memory, but when it reloads it back into the floating point
register, there is extra garbage in the low order bits since the FP
register
is much wider than the dword. VS 2005 C++ compiler gets around this by
storing the intermediate values in a qword memory space.

Having said this, this is still somewhat dangerous code to use. Checking
for
equality between floating pointer numbers is frought with danger because
of
implicit numerical roundoff when fractions are computed within the FP
registers and truncated to fit the register.
Yes, I'm aware of the danger, but, intuitively, you would expect identical
starting values and identical operations to produce identical results, even
in floats. In the real program the calculation is more involved, but the
inputs are longs and ints, not floats.

David

Sep 22 '06 #3
David W wrote:
"Brian Muth" <bm***@mvps.orgwrote in message
>The best advise is to upgrade to VS 2005 which doesn't exhibit this
problem.

Quite an expensive fix, not to mention the time involved in getting a
large project rebuilt with a new compiler. No 2003 service pack that
fixes it? If there isn't there should be, given the outlay.
Service pack 1 for VS2003 was recently released [1]. Have you tried it?

If it doesn't fix the problem, your only other option is to call Product
Support to see if there's a hotfix available.

[1] http://support.microsoft.com/?kbid=918007

-cd
Sep 22 '06 #4
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:ur**************@TK2MSFTNGP04.phx.gbl...
David W wrote:

Service pack 1 for VS2003 was recently released [1]. Have you tried it?
No.
If it doesn't fix the problem, your only other option is to call Product
Support to see if there's a hotfix available.

[1] http://support.microsoft.com/?kbid=918007
Thanks.

DW

Sep 24 '06 #5

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

Similar topics

6
by: asmirnov1234567890 | last post by:
Hi my python 2.3.4 for windows refuse to execute line float("NaN"). It says: >>> float("NaN") Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid literal for...
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
1
by: Sergio | last post by:
Hi everybody, I have the following scenario. Several computers with shared disk in a LAN. Each of these computer has a MySQL server that serves several databases. I have several clients that...
4
by: Jim West | last post by:
Both int a; std::complex<float> b; std::complex<float> c = static_cast<float>(a)*b; and int a; std::complex<float> b;
1
by: Peter Knörrich | last post by:
Hello, I've found another inconsistency, and looking through the list archives I can find mentions of funky stuff like print float('inf') giving Infanity
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
13
by: helen.m.flynn | last post by:
Hi I'm very much a beginner with Python. I want to write a function to convert celcius to fahrenheit like this one: def celciusToFahrenheit(tc): tf = (9/5)*tc+32 return tf I want the answer...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.