|
-
April 27th, 2007, 03:20 PM
#16
Re: .11 * 100 trouble in C++
-
April 27th, 2007, 03:27 PM
#17
Re: .11 * 100 trouble in C++
 Originally Posted by kyoryu
At any rate, casting from a float to an int is something you need to be careful with. You need to think about what you want the output to be for non-integer results. As an example, do you want 10.5 to be converted to 10, or 11?
Just to clarify, what I want to do is this: Give are set of float values essentially with 2 decimal places. The goal is eliminate the decimal point and get the integer value, which means simply multiply be 100.
12.34 -> 1234
Now I am going to eliminate float i think and store th original value in doubles.
-
April 27th, 2007, 03:31 PM
#18
Re: .11 * 100 trouble in C++
On second thought:
First solution (rely on rounding):
(int)(.11 * 100 + .005) should always produce 11
(int)(.9 * 10 + .05) should always produce 9
(int)(.359 * 1000 + .0005) should always produce 359
It would be less complicated to do it this way:
(int)(.11 * 100 + .5)
(int)(.9 * 10 + .5)
(int)(.359 * 1000 + .5)
-
April 27th, 2007, 03:38 PM
#19
Re: .11 * 100 trouble in C++
 Originally Posted by zspirit
Just to clarify, what I want to do is this: Give are set of float values essentially with 2 decimal places. The goal is eliminate the decimal point and get the integer value, which means simply multiply be 100.
12.34 -> 1234
Now I am going to eliminate float i think and store th original value in doubles.
How are you reading the information in in the first place? Given that you always want to store the information as an int, it may be easier to never actually use a float.
How reasonable this is is dependent on how you're getting the data.
Though, whether you store as float or double, you'll be better off if you always use the same data type for all caclulations, and then convert to int at the end of it all.
See the code I posted for an example of this.
-
April 27th, 2007, 04:26 PM
#20
Re: .11 * 100 trouble in C++
Here is the solution!!!
From the original post:
Code:
// case 2
float fVal = .11;
short int test = (short int) (float) (fVal * 100); // now this right!
The trick is to typecast it first to float and than to short int!!! ahhh
if we don't typecast to float, the result is treated as double and with high precision its value becomes .1099999, multiply this by 100 is 10.9, typecast this to int gives 10
typecasting 10.9 to int would simply discard the .9 part but typecasting it to float first will still work with .9 part because it is float and its value is read as 11.
-
April 27th, 2007, 04:55 PM
#21
Re: .11 * 100 trouble in C++
 Originally Posted by zspirit
Here is the solution!!!
From the original post:
Code:
// case 2
float fVal = .11;
short int test = (short int) (float) (fVal * 100); // now this right!
The trick is to typecast it first to float and than to short int!!! ahhh
if we don't typecast to float, the result is treated as double and with high precision its value becomes .1099999, multiply this by 100 is 10.9, typecast this to int gives 10
typecasting 10.9 to int would simply discard the .9 part but typecasting it to float first will still work with .9 part because it is float and its value is read as 11.

It don't work
try declaring fVal like double directly
Sorry, my english is not good
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
|