Re: Why does 1.2 % 0.01 not equal to zero?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hey Pete --

My integer method is sort of a back door cheat. It avoids the real
issue of imprecise binary representation of rational numbers. If the
question was posed by a student who needs to convince their instructor
of sufficient understanding ... I much prefer your approach. If on the
other hand it is part of a control/descision block of code ... I
prefer the simplification my method represents and it is one I have
used several times. I consider double % double a trap of sorts that
introduces results often forgotten by those who have not used it
recently. Neither approach is bullet proof; however, the inconsistent
results of the % operator on floats or doubles is so insidious ... I
have a hard time convincing myself that it is a good feature within
C#.

What happens when one has >>

1.20000001 % 0.01

In the above case the 0.00000001 remainder might be very important?
So checks against the RHS value of 0.01 are not a good practice. In
this case the multiplier would need to be 100,000,000 and
Int32.MaxValue = 2,147,483,647 issues start to emerge.

Eventually even the 128 bit decimal representation fails to provide
sufficient accuracy. Irrational numbers such as 1/3'rd and Pi are not
accurately represented even with decimal utilization. I think it false
to make statements such as: "Usage of decimal type assures
_sufficient_ accuracy."

Int32.MaxValue = 2147483647
Int64.MaxValue = 9223372036854775807
decimal.MaxValue = 79228162514264337593543950335

In summary:

1) A thorough understanding of the entire mathematical methodology
used and its inherent precision is necessary. A deep understanding of
the problem domain is mandatory and this is not a trivial task. It
requires the researcher(s) (physicist, engineer, mathematician, etc.)
and the computer scientist to work together.

2) If you see float % float or double % double in code ... become
very concerned.

-----------------------------------------------

My observed speed enhancement converting to integers was associated
with huge stock data files where the data was fully loaded into ram to
avoid hard drive i/o speed issues. In this specific case there was a
lot of numerical summation work being done (integrations, averages,
statistics, etc.) in a dynamic tuning algorithm with lots of looping
within the optimization routine.

Once values such as 987.125 were converted to 987125 the rewritten
program ran in approximately 1/5'th the time as the program using
doubles for data storage. I was thrilled to have results in 12 minutes
vs 1 hour. Situations where such improvements can be had are rare ...
but in my case it involves code in continuous operation and is one of
the best performance enhancements I have stumbled onto. In the texts
and courses I have been exposed too ... the speed differences between
integer math and doubles (or floats) has been ignored. (I have never
learned assembly nor been exposed to the very low level compiler
optimization methods.)

I encourage those where performance is critical to evaluate the
possibility of using integers. The conversion is not a simple process.
It is tricky enough where I suggest side-by-side testing between
integer code and doubles code until all ambiguities are understood and
removed.

-- Tom




.



Relevant Pages

  • Re: Representation.
    ... In my initial post I opened with the comment implying that I was entirely in tune with your rejection of FOR and HOR approaches. ... I am also entirely in agreement with the interpretation of representation that implies presentation to something - unlike in a computer where it is unclear what this might mean. ... The trouble is that the usual models can be shown to be physically incoherent using just the sort of logic you use to reach your model of representation. ... the term has no coherent meaning. ...
    (sci.psychology.consciousness)
  • Re: is order urgent doubt
    ... representation used is not IEEE compliant; ... represented on this system as the sum or difference of two doubles, ... This is equivalent to a 107 bit mantissa with an 11 bit biased ... is not fixed (there can be arbitrarily many virtual zero bits between ...
    (comp.lang.c)
  • Re: Report does not sort by date
    ... Sounds to me as if it's sorting on a text representation of the date, rather than the underlying representation that Access uses for Dates. ... You could get the same result if you stored the date "correctly", but used in the report after converting it to a string using one of the many functions which can be used to manipulate date values. ... create a report and sort by date in the report =- it does not sort correctly. ...
    (microsoft.public.access.gettingstarted)
  • Re: is order urgent doubt
    ... Long double arithmetic is supported by the compiler. ... represented on this system as the sum or difference of two doubles, ... Long double constants are coded as double precision constants followed ... require a double denormal in their representation and therefore ...
    (comp.lang.c)
  • Re: OT:Another realignment of reality...
    ... know I and the people I hang around do not end up needing legal ... representation of any sort, personal or hiring others. ...
    (comp.sys.mac.advocacy)