|
-
October 20th, 2009, 11:58 PM
#1
identifying integer assigned to double, how to?
hi,
The code iam working on accepts numeric input from the user and stores it into a double variable. The user can enter either floating point numbers or integers. How do i identify if the user has entered a floating point type or an integer type?
I've thought up the following code to figure out if the given number is of floating point type:
Code:
if( floor (doublevar) < doublevar )
//doublevar is of floating point type
this seems to work fine, is it correct? How do i check if the given number if of integer type?
thanks in advance
-
October 21st, 2009, 12:03 AM
#2
Re: identifying integer assigned to double, how to?
You could accept the input as a string, then check if there is a decimal point in there.
-
October 21st, 2009, 11:00 AM
#3
Re: identifying integer assigned to double, how to?
I would ask, why do you need to know if 2 is stored as 2.0? You can just work with the value as a float regardless can't you? The snippet you posted seems pretty creative. If you really needed to know, that method looks fine to me and seems more efficient than parsing a string.
-
October 21st, 2009, 11:12 AM
#4
Re: identifying integer assigned to double, how to?
If you really need to know (kempofighter is right, you really don't), you can use a union to store the float in a unsigned long (don't cast it!), then check for the IEEE formatting.
-Erik
-
October 22nd, 2009, 01:04 AM
#5
Re: identifying integer assigned to double, how to?
hi,
This is part of a calculator, right now the input goes into a double value and the result is also a double value, so 2*2 ends up being 4.000..., which is correct but not very pretty. So i need to figure out a way to represent integer results as integers.
i need to know if the user entered an integer so that i can display the output as an integer without decimal followed by 0s. for instance the result of 2*2 should be displayed as 4 and not 4.000000...,
the code snippet fails if the value of the decimal part is 0000000000000001 for instance if i store 1.0000000000000001 into doublevar, the if condition fails, why? i dont know yet , i'll have to deal with this once i've figured out how to extract the integer value if the user has entered an integer into the double value.
how can i extract the integer value from the double variable if the user has entered an integer? sizeof(int) is 4 bytes, sizeof(double) is 8 bytes. i need to extract the integer and then check it for range -2147483648 to 2147483647. If i cast the double value to int explicitly is there any chance i'll lose data?
thanks again for the replies
Last edited by austinium; October 22nd, 2009 at 01:11 AM.
Reason: added details
-
October 22nd, 2009, 03:33 AM
#6
Re: identifying integer assigned to double, how to?
 Originally Posted by austinium
how can i extract the integer value from the double variable if the user has entered an integer? sizeof(int) is 4 bytes, sizeof(double) is 8 bytes. i need to extract the integer and then check it for range -2147483648 to 2147483647. If i cast the double value to int explicitly is there any chance i'll lose data?
thanks again for the replies 
I think the problem you're trying to solve is much more involved than what you originally planned on.
First, what is the difference between 2.0 and 2? Which one is "floating point" and which one is "integer" in the context of what you're trying to do.
Secondly, floating point arithmetic using the usual methods (IEEE floats for example) is not exact. This is a fundamental fact of computer science.
http://www.parashift.com/c++-faq-lit...html#faq-29.16
Third, you have to do your entire input as strings and write your logic based on this. Forget about what the underlying representation of doubles and integers are, your requirements for input are too strict to be done reliably using ints and doubles. You need to use strings (here is a string, here is another string, now you determine what is what by inspecting the string).
Lastly, there are fixed point libraries already built that can take strings and do "math" on these strings of data. If you leverage these libraries into your calculator program, then that is much easier. Otherwise you need to create a fixed point library that does the math exactly (see point #2 above), and how to do that cannot be fully explained in a post on a message forum.
Regards,
Paul McKenzie
-
October 22nd, 2009, 10:23 AM
#7
Re: identifying integer assigned to double, how to?
If you want to see an example of a fixed point library, I have an article:
http://www.codeguru.com/cpp/cpp/algo...le.php/c12097/
-Erik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|