Solved

Casting floating numbers

Posted on 2003-12-02
16
436 Views
Last Modified: 2010-04-02
I am having trouble casting the following numbers. I am just trying print intergers. So, 1, 3, 42, 80, 4. If I cast those numbers to int, I have 1, 2, 41, 79, 4. Or is there any trick?

 1.00021
 3.00
 42.00
 80.00
 4.00

Anybody has any idea?
0
Comment
Question by:dkim18
  • 6
  • 5
  • 5
16 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9861398
@dkim18:

> Anybody has any idea?

Casting as an Int truncates, but you want to round, so add .5 to it before the cast.
So try this:
  int nVal = (int)(nFloat + 0.5f);

Hope That Helps,
Dex*
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 9861418
>>If I cast those numbers to int, I have 1, 2, 41, 79, 4.

It seems that the display is not what the values actually are. Casting should always work, e.g.

float f = 80.00;
int n = (int) f;

However, you might have to take the machine accuracy (eps) into consideration, e.g.

#include <iostream>
#include <limits>

using namespace std;

float f = 80.00;
int n = (int) f + numeric_limits<float>::epsilon();
0
 
LVL 86

Expert Comment

by:jkr
ID: 9861428
>>Casting as an Int truncates, but you want to round, so add .5 to it before the cast.

Hm, wouldn't that make the following incorrect?

float f = 80.70;
int n = (int) f + 0.5f;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 19

Expert Comment

by:Dexstar
ID: 9861508
@jkr:  Yes, what you listed is incorrect, but that's not what I said to do.  I said to do it like this:
     int n = (int) (f + 0.5f);

Which works just fine.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9861541
OK, then, what would be the result of

int n = (int) (88.70 + 0.5f);

?
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9861567
After that line, n will have the value of 89.

88.70 + 0.5 = 89.20, which, when truncated, gives you 89.

Am I missing something?

Dex*
0
 
LVL 86

Expert Comment

by:jkr
ID: 9861592
>>Am I missing something?

As the Q was about casting, not rounding...
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9861624
Yeah, that's true, and the question wasn't totally clear, but what I understood was that they expected 1, 3, 42, 80, 4 but were getting 1, 2, 41, 79, 4 instead...  Which looks like a rounding problem to me.  Maybe I'm way off.

Dex*
0
 

Author Comment

by:dkim18
ID: 9861733
Thanks you for your comments.
As I said, it is not rounding problem. I just want to print out integer only, but castinig is not working.
-----------------------
    float keno_float_1 = 0.00;
    float keno_float_2 = 0.00;
    float keno_float_3 = 0.00;
    int keno_int = 0;
    while(order < 6)
   {
     keno_float_1 = keno_array[from_top];
     keno_float_2 = float(int(keno_array[from_top]))*1.00;

     cout<<keno_float_1 <<", "<<keno_float_2<<endl;
     keno_float_3 = keno_float_1 - keno_float_2;
     cout<<"keno_float_3 "<<(int)(keno_float_3*100.00)<<endl;
...
...
---------------------

99.01, 99
keno_float_3: 1
87.61
69.03, 69.00
keno_float_3: 2
61.06
58.42, 58.00
keno_float_3: 41
51.33
45.80, 45.00
keno_float_3: 79
39.82
37.04, 37.00
keno_float_3: 4
32.74
-----------
From above, casting is not working...,but the first one fifth one are working.
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 200 total points
ID: 9861876
Geez, I had to stare at that for a while to figure out what you were trying to do.

Try this:
      cout<<"keno_float_3 "<<(int)((keno_float_3+0.5f)*100.00)<<endl;

Or maybe it is:
      cout<<"keno_float_3 "<<(int)((keno_float_3*100.00)+0.5f)<<endl;

I can't tell without trying it out.  If neither of those work, would you please post the values from keno_array that you are using?

Dex*
0
 
LVL 86

Expert Comment

by:jkr
ID: 9861898
Have you tried adding 'numeric_limits<float>::epsilon()'?
0
 

Author Comment

by:dkim18
ID: 9862743
I have tried like this 'numeric_limits<float>::epsilon()',

     keno_float_1 = keno_array[from_top];
     keno_float_2 = float(int(keno_array[from_top]))*1.00;
     keno_num = keno_float_1 - keno_float_2;


     cout<<endl;
     //prints number
     int n = (int) keno_num + numeric_limits<float>::epsilon();

but I was having the following problem.
-----
AnalyzeOutput.cpp: In function `void Write_Analyzed_File(char *, char *)':
AnalyzeOutput.cpp:193: `numeric_limits' undeclared (first use this function)
AnalyzeOutput.cpp:193: (Each undeclared identifier is reported only once
AnalyzeOutput.cpp:193: for each function it appears in.)
AnalyzeOutput.cpp:193: parse error before `>'
------

Dexstar,
your solution worked this part, but didn't work for the other part.(two of numbers were incorrect.)
Sorry for my messy code. I was experimenting to solve this problem.
0
 

Author Comment

by:dkim18
ID: 9862801
I wasn't sure how to use 'numeric_limits<float>::epsilon()' method.
Indeed,
#include <limits>
using namespace std;
didn't worked.
this gives me error, so I had to use #include <limits.h> even though I use for others like this
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <limits.h>

using namespace std;

By the way, I am using Sun Solaris system.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9862972
@dkim18:  What are the values for keno_array[from_top] that you are using?
0
 

Author Comment

by:dkim18
ID: 9863007
Please hold your answers. Let see if this works
0
 

Author Comment

by:dkim18
ID: 9863040
I solved problem!
Basically, Dexstar,'s method worked.
Thank you all.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

825 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