Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,696 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,494 people online right now. Registration is fast and FREE... Join Now!




Problem with fmod() function

 
Reply to this topicStart new topic

Problem with fmod() function, C

jaredolympia
post 18 Nov, 2005 - 04:30 PM
Post #1


New D.I.C Head

Group Icon
Joined: 15 Oct, 2005
Posts: 29



Dream Kudos: 75
My Contributions


fmod(x, y) returns the remainder of x/y. However, as I found out, it doesn't return the exact remainder which is a pretty big problem.

For instance...

You can go ahead and compile this code. It shoudn't give you any warnings or crash your system.

CODE
#include <stdio.h>
#include <math.h>

int main(void)
{
   double x = 0.55, y = 0.1, z;
   
   z = fmod(x, y);
   
   printf("z = %f\n", z);
   
   /* z = 0.05 if six digits of precision is displayed */
   /* however, z is actually greater than 0.05 which is a problem */
   /* here's why... */
   
   if(z == 0.05)
   {
        puts("Z is equal to 0.05");
   }
   else
   {
       puts("Z is not equal to 0.05");
   }
   
   /* In other words, the fmod() function does not return the perfect remainder. */
   
   system("PAUSE");
   return 0;
}


I need to be able to rely on fmod() returning the exact remainder. If anybody has an idea of a way around this problem, it would be greatly appreciated.
User is offlineProfile CardPM

Go to the top of the page


Vextor
post 18 Nov, 2005 - 11:58 PM
Post #2


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


I think I may have found something for you.

CODE

public: static Decimal Round(Decimal d, int decimals);


Where d is the decimal number to be rounded and Decimals is the number of places.

This may or may not help - but it's worth a try. smile.gif

This post has been edited by Vextor: 18 Nov, 2005 - 11:58 PM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Nov, 2005 - 07:42 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


There is no Decimal type in c programming...is it part of another struct or class you have written?
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 08:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month