You are here: Re: problem with fmod() « PHP Programming Language « IT news, forums, messages
Re: problem with fmod()

Posted by Jerry Stuckle on 11/30/07 12:01

Erwin Moller wrote:
> Tim Roberts wrote:
>> bummerland@gmail.com wrote:
>>> I have a problem with the function fmod.
>>>
>>> Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
>>
>> Because floating point arithmetic is not exact. The number "5.7"
>> cannot be
>> represented exactly in binary. So, it's quite likely that you are really
>> computing something like fmod(5.69999999999999, 0.1), and that result
>> would
>> be 0.09999999999999, which will print as 0.1.
>>
>> It's not the function that is broken, I am afraid. It is your
>> expectations.
>
> Hi Tim,
>
> The same thought crossed my mind.
> But what is the use of fmod if it uses unexcact representations internally?
> I mean, if you offer such a function, I would expect it does what it
> claims to do (eg in their examples).
> And thus NOT use 'normal' floating points representation, but something
> that respects 6.0 as 6.0 and not 5.9999999999999.
> (I must admit I don't know HOW, but I am sure some smart guy can figure
> something out.)
>
> Wouldn't it be better to dump fmod al together?
> I mean, what to expect from the function?
> What is your opinion?
>
> Regards,
> Erwin Moller
>

Erwin,

It's a limitation of all floating point calculations. And how can you
correct 5.99999999999 to 6.0 and not be sure the former was correct?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация