Re: Extremely wierd problem I just cannot explain...works in debug, does not work in release.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 6 Nov 2006 09:21:50 -0800, "Bruce Wood" <brucewood@xxxxxxxxxx>
wrote:

I agree with Adam: comparing floats / doubles for equality is a big
no-no.

Furthermore, a float or double should be used to store _only_ measured
quantities for which the concept of error makes sense. For example, you
could use a float or double to store "35.1523mm of rain", because if
you you're going to compare that quantity to something else, a
statement like "within 0.00001mm of yesterday's rainfall" makes sense.

You should never use floats or doubles to represent quantities or
things where precise values matter. The classic example is using them
to store monetary values (a quick road to losing / gaining pennies here
and there). So, I have to wonder exactly what "address" is... I have
the sneaking suspicion that losing a digit on an "address" would be
disastrous....

In cases in which your application can't tolerate precision loss you
should use decimal. If the number of decimal places is fixed then you
may also be able to use int or long and just remember how many decimal
places there are.

Adam Clauss wrote:
float address = entry.Address;
address += (float)entry.Bit/10.0f;
if(addr == address)The thing that catches my eye is that you are testing
equality on floating point values. That is generally a big no no as
floating point values are typically NOT exact.

If you had a "logical" value of 10.0, it might actually come out to
10.0000000001 or somethign along that line. Thus the test for equality
would fail. The better way would be to substract the two and take the
Math.Abs() - see if that value is less than a certain 'tolerance' level
(aka: they are close enough that you know they are actually the same).


Ya know what guys, I didn't even think about that....

I am definitely going to check that tomorrow!

Thanks for the heads up!

--
Stephan
2003 Yamaha R6

kimi no koto omoidasu hi nante nai no wa
kimi no koto wasureta toki ga nai kara
.



Relevant Pages

  • Re: Rounnding Issue
    ... In the database, if the datatype is a float, it will not contain EXACTLY 20.45 because that value CANNOT be stored in a float. ... A float is a binary fraction and asking a binary fraction to store 20.45 is like asking a decimal fraction to store 1/3. ...
    (microsoft.public.sqlserver.programming)
  • Re: Float value changes unexpectedly when performing math on field
    ... Float fields, by definition, have some built-in imprecision. ... This is a side effect being able to store very large numbers in smaller spaces. ... So, ideally, if that is not what you want, you should NUMERIC or DECIMAL, but not float. ... rollover to zero so I have a rollover count and rollover point so I can ...
    (microsoft.public.sqlserver.server)
  • Re: storing floating point data with using float or double
    ... I don't intend to store all of the 100,000 datapoints, but rather a subset ... I don't need the precision of a float, the above values would be fine to ... A couple of ideas I have come up with is to try compressing the array of ... though about breaking the values into an sbyte and a byte like this: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: storing floating point data with using float or double
    ... You can store the values as fixed point values in shorts. ... You would be able to get a precision of three decimals if you'd like. ... The numbers you could store this way would range from -32.768 to 32.767. ... Recently it was requested that I add the ability to generate graphs from the raw, un formatted test results (100,000+ float values) ...
    (microsoft.public.dotnet.languages.csharp)