Thread: strtol with floating point

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    201

    strtol with floating point

    I have a problem that I don't understand. consider the following code:

    double Scale;
    char *fmterror;

    Scale = strtol(string, &fmterror, 10);

    if ( *fmterror != '\0' || Scale == 0.0 )
    Scale = 1.0;
    else
    Scale /= 100;

    it was compiled with Builder5 on windows2000 and it gives a "floating point overflow" on the line "Scale /= 100". The debugger shows that Scale = 150 at that point and 150 / 100 seems all right to me.

    When I saw this I noticed that the code does do strtol instead of strtod and after changing double Scale into float Scale it worked. double is 8 bytes on this compiler and float 4 bytes, long also 4 bytes.

    What could be the problem with this code when Scale is double?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since you're actually trying to use floating point numbers, how about dividing by one?
    Code:
    Scale /= 100.0;
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Yes, that also didnt fix the problem

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      double  d;
      char    *p;
    
      d = strtod("100.12345", &p);
    
      if (*p != '\0' || d == 0.0)
      {
        d = 1.0;
      }
      else
      {
        d /= 100.0;
      }
      
      printf ("%f\n", d);
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM