Thread: Basic floating point arithmetic not working...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    11

    Basic floating point arithmetic not working...

    Hello,

    I am trying to run the following code (which is part of a larger program):

    Code:
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <time.h>
    #include <fstream>
    #define rand01 (0.9999999*double(rand())/RAND_MAX) 
    				
    #define getrandom(max1) (((rand())%(max1))) // random integer between 0 and max-1
    	  
    using namespace std;
    
    int main()
    {
    	double result;
    	int sec = 2;
    	int t = 10;
     	result= 2.0*3.14*(46/1000)*(sec*1000.0+t);
    
    	cout << result << "\n";
    
    	return 0;
    }

    My output for result is "0." This is incorrect. Anyone have any ideas?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that due to integer division, the result of (46/1000) is 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  3. Floating point operations
    By kjc197 in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 06:44 AM
  4. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM