<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Mar 1, 2010, at 2:32 PM, ik wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><br clear="all"><a href="http://ik.homelinux.org/">http://ik.homelinux.org/</a><br> <br><br><div class="gmail_quote">On Mon, Mar 1, 2010 at 23:20, Jerry <span dir="ltr"><<a href="mailto:lanceboyle@qwest.net">lanceboyle@qwest.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <div style="word-wrap: break-word;">Don't compare floating points. Ever. Now you see why.</div></blockquote><div><br>So if I need for example to compare currency or something that is floating point, how do I do that ?<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><br></div><div>Also, don't write loops that calculate a floating point number which is then tested for equality to exit the loop. Also, it is a bad idea to test for > or < for exiting unless you know in advance an accidental equality result will not yield surprising results. In other other words, don't do that (test for > or <) either.</div> </div></blockquote><div><br>As I understand, there is an old way to calculate floating point that is very popular and newer ways for that.<br>The old way uses binary calculation and the newer I do not understand them :)<br></div></div></div></blockquote><div><br></div>The older way that you are thinking of might be binary coded decimal, BCD, in which each decimal digit is represented by its own bit string, usually four bits, to represent the digits 0–9. It is rarely used any more. I'm not sure but maybe Hewlett-Packard calculators use BCD. (HP calculators are known for their excellent numerical accuracy. The academic who did a lot of their work was also involved in the IEEE 754 standard, I think.)</div><div><br></div><div>The newer way that you are thinking of is probably IEEE 754 floating point which is implemented widely on modern microprocessors. Its whole numbers (1.0, 2.0, etc.) are supposedly exact equivalents to integer counterparts up to a very large number, but you had _really_ better know what you are doing if you depend on that for comparisons. And expect portability problems when moving to non-IEEE 754 platforms.</div><div><br></div><div>BTW, the way to write a loop that increments a floating point number properly is this (Please pardon if any Ada syntax slips in here):</div><div><br></div><div>for i := 0 .. 100 begin</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a_float := 1.23 * i;</div><div>end;</div><div><br></div><div><br></div><div>whereas this is a bad idea:</div><div><br></div><div>a := 0.0;</div><div>while a <= 100.0 do</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a_float := 1.23 * i;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a := a + 1.0;</div><div>end;</div><div><br></div><div><br></div><div>as is this:</div><div><br></div><div><br></div><div>a := 0.0;</div><div>while a <= 123.0 do</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>a := a + 1.23;</div><div>end;</div><div><br></div><div>Jerry</div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_quote"><div> <br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><br></div><div>Jerry</div><div><br><div> <div><div></div><div class="h5"><div>On Feb 26, 2010, at 3:37 AM, ik wrote:</div><br></div></div><blockquote type="cite"><div><div></div><div class="h5"><div dir="ltr">Hello,<br><br>I've made a small test:<br><br>if (0.1+0.2) = 0.3 then<br> <br>The only compiler/interpator that actually tells that it equal is FPC.<br><br>I've tested it using Javascript (in Firefox), Ruby, Python, C (gcc) and Perl.<br> <br>Except FPC, everyone tells that 0.1+0.2 = 0.30000000000000004<br> <br>Can someone explain how FPC see the result and why others does not see it like that ?<br><br><br>Thanks,<br>Ido<br><br clear="all"><a href="http://ik.homelinux.org/" target="_blank">http://ik.homelinux.org/</a><br> </div> </div></div><div class="im"> _______________________________________________<br>fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a><br><a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a></div> </blockquote></div><br></div></div><br>_______________________________________________<br> fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a><br> <a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a><br></blockquote></div><br><br>Ido<br></div> _______________________________________________<br>fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a><br><a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a></blockquote></div><br></body></html>