Thread: Atof with scientific notation

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Here is information on the binary format of double (at least ones that follow the IEEE standard)

  2. #17
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Ok, I coded a simple representation of an extended Atof:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    
    #ifndef MAXSIZE
      #define MAXSIZE 1000
    #endif  
     
    enum bool {TRUE = 1, FALSE = 0}; 
    
    long double Atof(char* s)
    {
       enum bool isNegative = FALSE;
       long double val, power; 
       int i, sign, exp;
       
       /* while it's a space, keep iterating */
       for(i = 0; isspace(s[i]); i++) 
       ; 
       /* record the sign */
       sign = (s[i] == '-')? -1 : 1;
       
       /* if it is a sign, just ignore it iterating to the next index */
       if(s[i] == '-' || s[i] == '+') 
         i++; 
       
       /* get the power and the value for further manipulation */
       
       /* ----------------------------------------------------- */
       
       for(val = 0.0; isdigit(s[i]); i++) 
         val = 10 * val + (s[i] - '0');            
       if(s[i] == '.') 
         i++; 
       for(power = 1.0; isdigit(s[i]); i++) 
       {  
         val = 10 * val + (s[i] - '0');
         power *= 10;   
       }
       
       /* ----------------------------------------------------- */ 
       
       /* just ignore if 'e' or 'E' was entered */
       
       if(s[i] == 'e' || s[i] == 'E') 
       { 
         i++;
         /* test if the exponent is negative */
         if(s[i] == '-') 
            isNegative = TRUE;
         i++;
         /* test if the next char is a digit */
         if(isdigit(s[i]))
            exp = s[i] - '0';         
               
         /* if the exponent is negative, make the exp value negative
          * and return the proper value */
         if(isNegative) 
         { 
           exp = -exp;
           return (sign * val / power) * pow(10.0, exp);  
         }
         /* the exponent isn't negative, so don't make the exp value negative */
         else { 
           return (sign * val / power) * pow(10.0, exp);  
         }
       }
       /* 'e' or 'E' wasn't entered, just return the value with the adequate sign */
       else { 
         return val * sign / power;  
       }                              
    }
    
    int main(void)
    {
      char strnum[MAXSIZE];
      long double d;
      
      printf("Please enter a number in scientific notation of the form ");
      printf("[number]e-[exponent] or [number]E-[exponent]: ");      
      fgets(strnum, sizeof(strnum), stdin);
      
      d = Atof(strnum);
      printf("The double value in scientific notation is %Lf\n", d);
       
      return 0;   
    }
    I don't understand why even declaring my vars as long double, I can't get the precision offered by this type.
    Last edited by thames; 11-19-2012 at 08:06 AM.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you try adjusting the precision of your floating point answer? Here:
    Code:
    int main(void)
    {
      char strnum[MAXSIZE];
      long double d;
    
      printf("Please enter a number in scientific notation of the form ");
      printf("[number]e-[exponent] or [number]E-[exponent]: ");
      fgets(strnum, sizeof(strnum), stdin);
    
      d = Atof(strnum);
      printf("The double value in scientific notation is %18.18Lf\n", d);
    
      return 0;
      /*my output:
      Please enter a number in scientific notation of the form [number]e-[exponent] or
     [number]E-[exponent]: 2.33e-6
      The double value in scientific notation is 0.000002330000000000
      */
    }
    The number to the left of the decimal point is the width of the display you want, the number to the right is the precision you want to carry the value to. You can use the width to create space on your screen. Try not to go too crazy on the precision though. There is a hard limit on the number of digits supported by floating point types so there will be a point where you don't get any more precise. See <float.h> for details.

  4. #19
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Did you try adjusting the precision of your floating point answer?
    Many thanks. I overlooked that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scientific notation
    By manzoor in forum C++ Programming
    Replies: 6
    Last Post: 11-13-2007, 09:46 AM
  2. output is in scientific notation
    By scwizzo in forum Windows Programming
    Replies: 2
    Last Post: 09-18-2007, 10:37 AM
  3. Scientific notation
    By Lionmane in forum C Programming
    Replies: 9
    Last Post: 06-01-2005, 11:10 PM
  4. Reading scientific notation
    By PunkyBunny300 in forum C Programming
    Replies: 1
    Last Post: 04-24-2003, 09:22 PM
  5. Scientific Notation
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 03:08 PM

Tags for this Thread