Floating Point Conversion
By your friendly neighborhood Bilal.
isinteger() (validates a string of numbers if its an integer) �atol() (converts a string of numbers into a long integer)
Now we all (well most of us) completed assignment 2 and have used both of these.
Here is how it Worked.
Once a string is validated as an int we can call the atol function to convert it into a long integer!
cout << num << endl;
output: 355 as int not as string.
Now what if the string was a floating point number instead of an integer?���**Surprise it is!!
isfloat() (validates a string of numbers if its a floating point number) �atof() (converts a string of numbers into a floating point number)
We Use Different Functions For Floats.
So What Is The Problem?
These exist as library functions:
isdigit() library function.
This does not exist as a library function:
Isfloat()
Solution
A DECIMAL!
From isinteger() to isfloat()
bool isinteger(char w[])
{
bool result = true;
int start = 0;
if (w[0] == '-' || w[0] == '+') start = 1;
unsigned long int k = start;
while( !(w[k]=='\0') && result )
{
result = result && isdigit(w[k]);
k++;
}
return result;
}
From isinteger() to isfloat()
bool isinteger(char w[])
{
bool result = true;
int start = 0;
if (w[0] == '-' || w[0] == '+') start = 1;
unsigned long int k = start;
while( !(w[k]=='\0') && result )
{
result = result && isdigit(w[k])
|| result && (w[k] == ‘.’);
k++;
}
return result;
}
From isinteger() to isfloat()
bool isfloat(char w[])
{
bool result = true;
bool onedecimal = false;
int start = 0;
if (w[0] == '-' || w[0] == '+') start = 1;
unsigned long int k = start;
while( !(w[k]=='\0') && result )
{
if (w[k] == ‘.’ && !onedecimal) onedecimal = true;
else {
result = result && isdigit(w[k])
|| result && (w[k] == ‘.’); }
k++;
}
return result && onedecimal;}
Atof()
mov rdi, rsp
call atol
and no need to copy rsp to rdi because atof will return its value into xmm0 register
call atof
I Believe In You All!
But nonetheless any questions?