Solved

Question on checking for the size of a float value

Posted on 2004-09-21
5
217 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:DancingFighterG
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12118365
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.



 
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12120048
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
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12121297
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
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 25 total points
ID: 12126429
Your parsing is a bit loose, what will your code do with the perfectly valid numbers:

1.0e+38

1.0e0000000000038

I'd suggest this approach:

(0) Get a leading plus or minus, if not found, assume plus.
(1) Get a digit string, if none found, error out.
(2) If there's a dot, get another digit string, if not found, assume .00000
    (2a) get a letter, if it's 'E' or 'e':
           (2a.a)  get a sign, if not foiund. assume plus.
           (2.a.b) get a digit string, if not found, error out.

------------------
That's a much more bulletproof way to parse a real constant.

 
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 25 total points
ID: 12129428
addendum to grg99's suggestion:

(0) Get a leading plus or minus, if not found, assume plus.
(1) Get a digit string, if none found, error out.
(2) If there's a dot, get another digit string, if not found, assume .00000
    (2a) get a letter, if it's 'E' or 'e':
           (2a.a)  get a sign, if not foiund. assume plus.
           (2.a.b) get a digit string, if not found, error out.
    (2b) if something other than 'E' or 'e' or white chars, error out
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now