float value returning: 1.#INF

This is a discussion on float value returning: 1.#INF within the C++ Programming forums, part of the General Programming Boards category; Code: { float MPH, hours; int Miles; Miles = 37 + ((15000/5280) + (12000/YARDS_IN_MILES)); hours = 45/60; MPH = Miles/hours; ...

  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,185
    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
    Katy, Texas
    Posts
    2,309
    The result of this expression - dividing an integer 45 by an integer 60 yields zero. Then, you divide Miles by zero.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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,893
    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,893
    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, 01: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, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21