Thread: float to long conversion issues?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    float to long conversion issues?

    GNU GCC compiler

    Hello,

    I was going through some C exercises when I ran across one that I was having a lot of trouble doing. As it turns out the author's code has an issue and I am stuck on that. The cent value can be off by 1 on certain numbers. It does not appear to be random since it has been affecting the same numbers on Windows and Linux.

    A couple of the numbers that I see turn out wrong are: 4.99, 5.99, 1000.99 1000000.99

    My biggest question is why does there seem to be a rounding error?


    Thanks,
    Carl

    The short description: Take a monetary value input as variable type float. Put the dollars in a "long" and the cents into a "long" variable type. Then print the value back out using the "long" types.

    Author's Code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      float amounts[5];                     /* Stores data values        */
      long dollars[5];
      long cents[5];
    
      printf("Enter five monetary values separated by spaces:\n");
      for(int i = 0 ; i<5 ; i++)
        scanf("%f", &amounts[i]);
    
      for(int i = 0 ; i<5 ; i++)
      {
        dollars[i] = (long)amounts[i];
        cents[i] = (long)(100.0*(amounts[i]-dollars[i]));
      }
    
      printf("\n");
      for(int i = 0 ; i<5 ; i++)
        printf("  $%d.%02d", dollars[i], cents[i]);
    
      printf("\n");
      return 0;
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should round to the nearest cent.
    Code:
    dollars[i] = (long)(amounts[i] + 0.005);
    cents[i] = (long)((amounts[i] - dollars[i]) * 100 + 0.5);

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    read what KCfromNC posted. in the real world, you would not use floating point in currency calculations. unless you are required to read the data as float, you could read the dollars and cents separately with scanf("%d.%d",&dollars[i],&cents[i]); to avoid the floating point issues.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    After reading "What Every Computer Scientist Should Know About Floating-Point Arthmetic" I can say my head hurts. More importantly I can say I understand digits of precision better, there are different methods of implementing floating point, and most importantly that floating-point is an approximation.

    Of course, I now have another question. Where are floating point numbers advantageous?

    Thanks for all the help. It has been better than any book I picked up!

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    floating point numbers are advantageous when you need a wide dynamic range for computations. that is, integers have a fixed range of around +- 2 billion. but scientific computations can require a much wider range. but the tradeoff for the wider range is the need to approximate values. there are lots of scientific problems where you need numbers much larger than 2 billion.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Snaggletooth View Post
    Of course, I now have another question. Where are floating point numbers advantageous?
    ...
    Mostly in scientific computations where an (un)signed (long) integer falls short to store the magnitude.
    Some examples would be "mass of an electron", "parsec", or "Avogadro's constant" etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Float issues
    By Godfearn in forum C Programming
    Replies: 8
    Last Post: 12-16-2011, 12:39 AM
  2. trouble in long float
    By siperi in forum C Programming
    Replies: 4
    Last Post: 11-04-2010, 09:52 AM
  3. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM
  4. int, long, float???
    By N707JT in forum C Programming
    Replies: 1
    Last Post: 03-05-2003, 05:16 AM
  5. subtracting float from long
    By doylie in forum C Programming
    Replies: 0
    Last Post: 02-20-2003, 05:11 AM