Rounding
-
Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called
float_round_style
that includes something calledround_toward_infinity
... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool: -
Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called
float_round_style
that includes something calledround_toward_infinity
... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++
-
double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++
-
Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called
float_round_style
that includes something calledround_toward_infinity
... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool: -
I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++
-
I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++
-
ceil()
You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...
-
I find that very difficult to comprehend. Numbers should round up from .5 upwards, thought. That makes the floor function totally useless. This will work. #include double d = 49.99999956; int i = (int)ceil(d); Christian Graus - Microsoft MVP - C++
-
The Function "double ceil(double);" will always round UP. that is even if you have 49.00005 the ceil() function will round UP to 50; if you want to round to the nearest Integer you need somthing else. G_S
G_S wrote: The Function "double ceil(double);" will always round UP. I guess that's why it's called ceil ( as in ceiling ). G_S wrote: if you want to round to the nearest Integer you need somthing else. I was helping someone who wants to round UP. However, casting to int seems to just drop the floating point part, when I assumed it would round to the nearest int, up or down. HEnce, I suggested ceil. Christian Graus - Microsoft MVP - C++
-
Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called
float_round_style
that includes something calledround_toward_infinity
... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:double d = 49.99999999; int i = (int)(d + 0.5); i will now be properly rounded.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
double d = 49.99999999; int i = (int)(d + 0.5); i will now be properly rounded.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
The Function "double ceil(double);" will always round UP. that is even if you have 49.00005 the ceil() function will round UP to 50; if you want to round to the nearest Integer you need somthing else. G_S
To alter the processor's default rounding behavior, you can use the
_controlfp()
function to set specific FPU flags (such as_RC_CHOP
,_RC_UP
,_RC_DOWN
,_RC_NEAR
).You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...
-
Hiya I have a variable that shoud return a value of say... 50. Instead it's returning 49.9999999956 which may sound pretty good but I need the 50. I found this thing in the Std library which is an enum called
float_round_style
that includes something calledround_toward_infinity
... but I can't find how to actually use the durn code, an example or anything other than the description :mad:. Can anyone tell me how to use it? Is there any other easier way to acomplish what I want? Thanks!!! :cool:The method PJ showed, and you expanded for negative numbers, is a standard way. Shog hinted at another way doing it using _controlfp (or _control87). The rounding control flags specify how the assembly instruction frndint will round. From Intel info on frndint: The Control Word 16-bit register is used ... The RC field (bits 11 and 10) or Rounding Control determines how the FPU will round results in one of four ways: 00 = Round to nearest, or to even if equidistant (this is the initialized state) 01 = Round down (toward -infinity) 10 = Round up (toward +infinity) 11 = Truncate (toward 0) So, by default, it rounds the way you want. The problem is i don't know of any C function that calls this, hence the workaround of adding 0.5 and then truncating by cast to int. A more general (non-portable) solution would be to write your own function that calls frndint. e.g.
__forceinline double FkRound( double N )
{
__asm {
fld N
frndint
}
}Before calling this you could call _controlfp() to set the way you want rounding to work, or just to make sure it hasn't been changed by some other code. This works for both positive and negative numbers. ...cmk Save the whales - collect the whole set
-
double d = 49.9999999956; int i = (int)d; Christian Graus - Microsoft MVP - C++
Hello, The standard implies that when a floating point value is converted to a intergral value, the fractional part is discarded... I also got the blogging virus..[^]