Thread: float value returning: 1.#INF

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    float value returning: 1.#INF

    Code:
    {
    
    	float MPH, hours;
    	int Miles;
    
    	Miles = 37 + ((15000/5280) + (12000/YARDS_IN_MILES));
    
    	hours = 45/60;
    	MPH = Miles/hours;
    	
    	cout << "MPH: " << MPH;
    	return MPH;
    	
    }
    MPH couts 1.#INF -- not sure why... even if I use setprecision. Any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well: hours is set to 0.0 (since 45/60 is 0 remainder 45, and remainder don't count); and dividing by 0.0 gives 1.#INF, since the fancy infinity sign is hard to print.

    In other words: do hours = 45.0/60.0; instead.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The result of this expression - dividing an integer 45 by an integer 60 yields zero. Then, you divide Miles by zero.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Todd Burch View Post
    The result of this expression - dividing an integer 45 by an integer 60 yields zero. Then, you divide Miles by zero.

    Todd
    Also, 15000/5280 is equal to 2, as far as the compiler is concerned.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And as a metric user, I have no idea what YARDS_IN_MILES is, but if it's a integer, too, there's another bad calculation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1
    Quote Originally Posted by lord View Post
    Code:
    {
    
    	float MPH, hours;
    	int Miles;
    
    	metric conversion = 37 + ((15000/5280) + (12000/YARDS_IN_MILES));
    
    	hours = 45/60;
    	MPH = Miles/hours;
    	
    	cout << "MPH: " << MPH;
    	return MPH;
    	
    }
    MPH couts 1.#INF -- not sure why... even if I use setprecision. Any ideas?
    I;m looking into this.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This thread has been dormant for over 3 months. If the OP still had the problem, he'd have posted.

    Please don't revive old threads.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM