Link to home
Start Free TrialLog in
Avatar of DancingFighterG
DancingFighterG

asked on

Question on checking for the size of a float value

Hello, i'm tyring too check if an float value that someone enters is correct and does not exceed a certain range. Right now I'm trying to use E, where represnts the exponent to a power of 10 and digits represents 1 or more juxtaposed numbers. E can be the lower case, "e". There can be no more than 7 digits in any floating points number excluding the exponent (the decimal point does not count as a digit) Exponents are restriced to range of -38 to 38.

This is what I have right now:

      else if(out == 3)
            {
                  int exponent_check = 0;
                  std::istringstream(Lexeme.substr((Lexeme.length())-2 ,Lexeme.length())) >> exponent_check;      
                  if(((exponent_check >= 38) || (exponent_check <= -38)) && 
                     ((Lexeme.find("e") == ((Lexeme.length())-3)) || (Lexeme.find("E") == ((Lexeme.length())-3)) ||
                      (Lexeme.find("e") == ((Lexeme.length())-4)) || (Lexeme.find("E") == ((Lexeme.length())-4))) && Lexeme.find(".") != 0)
                  {
                        cout << "\n*** ERROR - Float Literal exponent is out of range ( -38 < x < 38 )" << endl;
//                        num_bad_real_constants++;
                        return 40;
                  }

I'm trying to check the the length then compare. The number that I am checking is the following:

1.35562127e1

This number should be throwing an error but it doesn't so I need to know what I am doing wrong in the check. Any help with this would be great.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

You can use logaritms. Decimal logarithms tells you the exponent of a floating number.
Make some tests with log() function.
Remember to #include "math.h" header file.



 
Avatar of sunnycoder
Hi DancingFighterG,

Perhaps you should have this question in the C++ TA. I can give you a C solution if you want. Alernatively, I can move this question to C++ TA. Since some experts visit both C and C++ TAs, may beyou can wait a while before they see and comment on this.

sunnycoder
Your exponent is within the range. That's why it is not throwing error.
But, I have got one suggestion for you: Divide your condition statement to different if statements. And don't do the calculation inside a condition statement. Make it as simple as possible. Then it will be easier to debug and understand and errors will be lesser.

-ssnkumar
SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial