1 of 13

Floating Point Conversion

By your friendly neighborhood Bilal.

2 of 13

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.

  • We use isinteger() to verify that our input was an integer.

  • We use atol() (or atolong) to convert the ascii characters of the string into a long integer.

3 of 13

Here is how it Worked.

  • String a = “355”;
  • String b = “123fdgh”;

  • Bool a_valid = isinteger(a); - returns true because string a is an int.
  • Bool b_valid = isinteger(b); - returns false because string b is not an int.

Once a string is validated as an int we can call the atol function to convert it into a long integer!

  • long int num = atol(a);

cout << num << endl;

output: 355 as int not as string.

4 of 13

Now what if the string was a floating point number instead of an integer?���**Surprise it is!!

5 of 13

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 isfloat() to verify our input was a floating point .

  • We use atof() to convert the ascii characters of the string into a floating point number.

We Use Different Functions For Floats.

6 of 13

So What Is The Problem?

These exist as library functions:

                  • Isinteger() by Professor Holliday

isdigit() library function.

                  • Atol()

                  • Atof()

This does not exist as a library function:

Isfloat()

7 of 13

Solution

  • Take Professor Holliday’s isinteger() function (or create your own) and include the only difference between integers and floats.

A DECIMAL!

  • Therefore instead of validating the input of just a digit ( ‘0’, ’1’, ’2’, ’3’, ’5’, ’6’ ,’7’ , ’8’, ’9’), as in isdigit() and isinteger(), include the input of a decimal ( ‘.’ )!

8 of 13

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;

}

9 of 13

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;

}

10 of 13

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;}

11 of 13

Atof()

  • Exactly the same way you call atol()
    • mov rax0

mov rdirsp

call atol   

  • Only difference is passing a 1 to rax instead of 0 to indicate the passing of a float number

and no need to copy rsp to rdi because atof will return its value into xmm0 register

    • mov rax1

call atof

12 of 13

13 of 13

I Believe In You All!

But nonetheless any questions?