473,738 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with fmod()

Hi,

I have a problem with the function fmod.

Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?

tia bummerland
Nov 29 '07 #1
7 2580
bu********@gmai l.com wrote:
Hi,

I have a problem with the function fmod.

Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?

tia bummerland
Hi,

Read the usercontributed notes on www.php.net for that function.
http://nl3.php.net/manual/en/function.fmod.php

It seems to be broken.
Also you'll find a few replacing algoritms.

Regards,
Erwin Moller
Nov 29 '07 #2
On 29 Nov., 13:18, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
bummerl...@gmai l.com wrote:
Hi,
I have a problem with the function fmod.
Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0?
tia bummerland

Hi,

Read the usercontributed notes onwww.php.netfo r that function.http://nl3.php.net/manual/en/function.fmod.php

It seems to be broken.
Also you'll find a few replacing algoritms.

Regards,
Erwin Moller
Thanks.
Nov 29 '07 #3
bu********@gmai l.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.69999999 999999, 0.1), and that result would
be 0.0999999999999 9, which will print as 0.1.

It's not the function that is broken, I am afraid. It is your
expectations.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 30 '07 #4
Tim Roberts wrote:
bu********@gmai l.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.69999999 999999, 0.1), and that result would
be 0.0999999999999 9, 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
Nov 30 '07 #5
Erwin Moller wrote:
Tim Roberts wrote:
>bu********@gmai l.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.69999999 999999, 0.1), and that result
would
be 0.0999999999999 9, 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.
js*******@attgl obal.net
=============== ===

Nov 30 '07 #6
Jerry Stuckle wrote:
Erwin Moller wrote:
>Tim Roberts wrote:
>>bu********@gmai l.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.69999999 999999, 0.1), and that result
would
be 0.0999999999999 9, which will print as 0.1.

It's not the function that is broken, I am afraid. It is your
expectation s.

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?
Hi Jerry,

I am aware of the the problems concerning internal representation of
floating point numbers.

If I say to YOU 6.0 you know I don't say 5.999999999, right?
But if you are a computer you represent 6.0 to something that could
resemble 5.99999999 more than 6.0.

I just wonder why PHP offers fmod() while we all know it won't work in
many/most cases as expected because of internal floating point storage
(and thus changing values).

So, as far as I can see:
1) PHP shouldn't offer fmod() as it is now.
or
2) PHP should offer a fmod() that uses some other math behind the scenes.
Maybe with old fixed-point representation?
Not sure.
(Erwin is not a mathematician)
There must be libs that address this issue.

My main problem with fmod() is that it seems quite useless in its
current form.

Regards,
Erwin Moller
Nov 30 '07 #7
Erwin Moller wrote:
Jerry Stuckle wrote:
>Erwin Moller wrote:
>>Tim Roberts wrote:
bu********@gmai l.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
represente d exactly in binary. So, it's quite likely that you are
really
computing something like fmod(5.69999999 999999, 0.1), and that
result would
be 0.0999999999999 9, 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?

Hi Jerry,

I am aware of the the problems concerning internal representation of
floating point numbers.

If I say to YOU 6.0 you know I don't say 5.999999999, right?
But if you are a computer you represent 6.0 to something that could
resemble 5.99999999 more than 6.0.
Sure. But that's how floating point calculations work. How would the
computer know the answer wasn't actually 5.99999999? It's outside the
available precision. And the same problem occurs in C/C++'s fmod()
implementation.
I just wonder why PHP offers fmod() while we all know it won't work in
many/most cases as expected because of internal floating point storage
(and thus changing values).
Don't know. I never found much good use for it anyway. Didn't in
C/C++, either.
So, as far as I can see:
1) PHP shouldn't offer fmod() as it is now.
or
I wouldn't miss it.
2) PHP should offer a fmod() that uses some other math behind the scenes.
Maybe with old fixed-point representation?
Not sure.
Very difficult to do. The first problem is both 5.7 and 0.1 cannot be
expressed in floating point. So to do it with accuracy, they would have
to get rid of the IEEE implementation of floating point altogether and
go do packed decimal or similar.

And that will make all calculations much more difficult, because
processors will handle the IEEE format transparently, but (other than
IBM mainframes) won't do packed decimal. So instead of one instruction
to do floating point add/subtract/multiply/divide, they would have to
have a whole routine to handle it.
(Erwin is not a mathematician)
There must be libs that address this issue.

My main problem with fmod() is that it seems quite useless in its
current form.

Regards,
Erwin Moller
Nope, no libs to handle it. It's a restriction of floating point.

Just like in decimal - 10 / 3 * 3 = 9.999999999, and not 10.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Nov 30 '07 #8

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

Similar topics

2
4629
by: Zunbeltz Izaola | last post by:
Hi, I have a problem with % operation. It returns a incorrect value in this case: >>> -2.77555756156e-17%1 1.0 where the returned value should be -2.77555756156e-17.
1
2406
by: Tian | last post by:
I am trying to use pyfmod in python to manipulate sound. i have installed pyfmod, ctype, numarray (if they are necessary), i also copied fmod.dll to python/DLLs as well as windows/system32, but when i tried import pyFMOD I got: Traceback (most recent call last):
8
5628
by: seia0106 | last post by:
Hello, I have a C++ program , which has following two lines of code z=atan2(x,y); z=(float)(fmod(2*pi+z, 2*pi); The comments written by the other programmer says that second line is used to extend the range of variable z from '-pi to pi' to '-2pi to 2pi'.
6
8101
by: stau | last post by:
Hi! I'm reading a C book, and it says that fmod() returns the remainder of the exact division of it's arguments. Well, in a exact division, the remainder shall always be 0 (zero), so this don't make any logic (I guess). Anyway, foward in the chapter it says that fmod returns the remainder of the integer division of it's arguments. I checked KnR 2nd ed. and the man page, and still can't figure out wich one is correct.
1
8120
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net. Does anyone has an idea on how to make this code compile in .Net ? Thanks, Joannes Vermorel
2
2625
by: Gintautas | last post by:
I'm trying to play a part of wav file. FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,0); plays all file FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,90000); plays file until 90000 sample FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 25000, 25000); dont't play anything any suggestions?
17
6403
by: joseph.p.doyle | last post by:
This code, compiled with visual studio .NET 2003, double a = 95.022, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a - floor(a / b) * b); a = 95.021, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a - floor(a / b) * b); a = 95.020, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a -
14
16597
by: Aaron Gray | last post by:
Does anyone have a good fmod() function written in Javascript ? Many thanks in advance, Aaron
17
6354
by: Jonathan Pritchard | last post by:
I know this is a simple problem, but I've just included this in the title because it explains what my program tries to do. The following does not work, it for someone reason does not want to place the values on a new line. ======================================================================= #include <stdio.h> #include <conio.h> int main(void)
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.