Alexander said:
You could, but sscanf can take care of that for you:
char *fpz = "3.9225147e-41";
double fp;
sscanf(fpz, "%lf", &fp);
printf("%e", fp); //3.9225147e-41
Forgot to mention that I can't use library functions to complete this task. I have completed the assignment... thanks for the help guys!
Solution in case you are bored/interested:
/*
Jason Campos
CS 47
T Howell
Homework Assignment #2
floatparts.c
This program deconstructs a list floating point numbers and displays their parts as:
sign(+/-) exponent(+/- 127) significand( 0 <= s < 2 )
This program assumes a 32 bit float representation and will not work under any other circumstances.
*/
#include <stdio.h>
#include <stdlib.h>
#define eBias 127
#define eMax 127
#define eMin -127
#define acceptInvalid 0 // set to zero if invalid entries are to be skipped, 1 to throw warnings.
int validateInput(char* fArg, float* fPtr);
int f2l (float f);
int getE(int i);
int getM(int i);
char getSign(int i);
int main(int argc, char* argv[])
{
if( argc < 2 )
{
printf("Usage: floatparts expects a list of floating point numbers in decimal or scientific format.\n");
printf("Example: floatparts .474747 3.9225147e-41 -3.4028235e+38 \n");
exit(0);
}
int i;
for (i = 1; i < argc; i++)
{
float currInput = 0.0;
printf("input %i = %s\n", i, argv[i]);
if (validateInput(argv[i], &currInput)) // returns 0 if input cannot be validated as a float
{
int iVal = f2l(currInput);
char sign = getSign(iVal);
int E = getE(iVal);
int Mr = getM(iVal);
int Ml = ( E > eMin) ? 1 : 0;
printf("sign = %c, exponent = %i, significand = %i.%06x\n\n", sign, E, Ml, Mr);
}
else
{
printf("Argument is not in a recognized floating point format\n");
}
}
}
/* Takes a string of characters representing a floating point number
and converts to an actual float. Stores the value in the space provided
by argument 2.
returns 1 if valid input, 0 otherwise
Valid characters for a floating point number are 0-9, +/-, e, and '.'.
Only one decimal can be used.
*/
int validateInput(char* fArg, float* currFloat)
{
*currFloat = 0.0; // reset
char* currChar = fArg;
int decimalSwitch = 0; // 0 if left side, 1 if right
int negativeSwitch = 1; // 1 or -1
double divisor = 10.0;
while(*currChar != '\0')
{
if(!(decimalSwitch)) // Left hand side of decimal accepts only digits or +/-
{
if (*currChar >= '0' && *currChar <= '9')
{
*currFloat = 10 * (*currFloat) + negativeSwitch * ( *currChar - '0');
}
else if (*currChar == '.')
{
decimalSwitch = 1;
}
else if (*currChar == '-' && *currFloat == 0.0)
{
negativeSwitch = -1;
}
else
{
if (acceptInvalid)
printf("Invalid character encountered: '%c'. Character skipped.\n", *currChar);
else
return 0;
}
}
else // Right hand side of decimal accepts digits and an 'e' preceded by a '+/-'
{
if (*currChar >= '0' && *currChar <= '9')
{
*currFloat += (negativeSwitch*(*currChar - '0') / (divisor));
divisor *= 10;
}
else if (*currChar == 'e' || *currChar == 'E')
{
/* This is an argument formatted as x.xxxe(+/-)exp */
int exp = 0;
int neg = 1;
currChar++;
if (*currChar == '-')
{
neg = -1;
}
currChar++;
while(*currChar != '\0')
{
if (*currChar >= '0' && *currChar <= '9')
{
exp = 10*exp + neg*(*currChar -'0');
}
else
{
if (acceptInvalid)
printf("Warning: Invalid character encountered as exponent: %c", *currChar);
else
return 0;
}
currChar++;
}
if (exp > 0)
{
int i;
for (i = 0; i < exp; i++)
*currFloat *= 10;
}
else if (exp < 0)
{
int i;
for (i=exp; i < 0; i++)
*currFloat /= 10;
}
return 1;
}
else
{
if (acceptInvalid)
printf("Warning: Invalid character encountered as exponent: %c", *currChar);
else
return 0;
}
}
currChar++;
}
return 1;
}
int f2l(float f)
/* convert float to long integer with same bit pattern */
{
union
{
float ff;
int l;
} a;
a.ff = f;
return a.l;
}
/* Mask all but most significant bit */
char getSign(int i)
{
int j = (i & 0x80000000);
j = (j>>31) + 1;
char c = (j==0) ? '-' : '+';
return c;
}
/* Mask bits 31 and 0-22 */
int getE(int i)
{
int j = (i & 0x7f800000);
j>>=23;
j = j & 0x000000FF; // in the case of a negative number, avoid leading 1s
j -= eBias;
return j;
}
int getM(int i)
{
int j = (i & 0x007fffff);
j <<=1 ; // fill empty bit
return (j);
}