Best way to approximate in direction to zero in C++
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I have some troubles explaining clearly but it's rather simple..
I have a double named value in my C++ program and I want to Floor it if it's a positive value and Ceil if it's a negative value, the precision is given by an external variable.
An example:
precision is of 1000
value is 0.2659 so approximated value is 0.265
value is -0.2659 so approximated value is -0.265
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
Here what I have so far:
void NodeImpl::approximation(double& value, const double& precision)
if (value > 0.0)
value = std::floor(value * precision);
else
value = std::ceil(value * precision);
value /= precision;
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
c++ double approximation
 |Â
show 7 more comments
up vote
9
down vote
favorite
I have some troubles explaining clearly but it's rather simple..
I have a double named value in my C++ program and I want to Floor it if it's a positive value and Ceil if it's a negative value, the precision is given by an external variable.
An example:
precision is of 1000
value is 0.2659 so approximated value is 0.265
value is -0.2659 so approximated value is -0.265
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
Here what I have so far:
void NodeImpl::approximation(double& value, const double& precision)
if (value > 0.0)
value = std::floor(value * precision);
else
value = std::ceil(value * precision);
value /= precision;
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
c++ double approximation
2
Looks ok to me but do note that the finalvalue
may have joke digits after the decimal 15th significant figure, e.g.0.265
is actually0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passingprecision
byconst
reference?
â Bathsheba
5 hours ago
3
Unrelated to your problem, but you don't need to passprecision
by constant reference. Just pass by value, or possibly constant value (i.e. plaindouble precision
orconst double precision
). I would also personally rather return the new value instead of using a reference argument.
â Some programmer dude
5 hours ago
2
be warned that your intended operation cannot be performed exactly: the number0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.
â Walter
4 hours ago
4
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
2
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for thedouble
type. In short, there's nothing gained. Generally, for primitive types (int
,double
, etc.) don't pass by reference unless you need to modify the value inside the function.
â Some programmer dude
3 hours ago
 |Â
show 7 more comments
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I have some troubles explaining clearly but it's rather simple..
I have a double named value in my C++ program and I want to Floor it if it's a positive value and Ceil if it's a negative value, the precision is given by an external variable.
An example:
precision is of 1000
value is 0.2659 so approximated value is 0.265
value is -0.2659 so approximated value is -0.265
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
Here what I have so far:
void NodeImpl::approximation(double& value, const double& precision)
if (value > 0.0)
value = std::floor(value * precision);
else
value = std::ceil(value * precision);
value /= precision;
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
c++ double approximation
I have some troubles explaining clearly but it's rather simple..
I have a double named value in my C++ program and I want to Floor it if it's a positive value and Ceil if it's a negative value, the precision is given by an external variable.
An example:
precision is of 1000
value is 0.2659 so approximated value is 0.265
value is -0.2659 so approximated value is -0.265
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
Here what I have so far:
void NodeImpl::approximation(double& value, const double& precision)
if (value > 0.0)
value = std::floor(value * precision);
else
value = std::ceil(value * precision);
value /= precision;
I have wrote a simple code but I was wondering if there was a more simple or/and way to do it.
c++ double approximation
c++ double approximation
asked 5 hours ago
tony497
999
999
2
Looks ok to me but do note that the finalvalue
may have joke digits after the decimal 15th significant figure, e.g.0.265
is actually0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passingprecision
byconst
reference?
â Bathsheba
5 hours ago
3
Unrelated to your problem, but you don't need to passprecision
by constant reference. Just pass by value, or possibly constant value (i.e. plaindouble precision
orconst double precision
). I would also personally rather return the new value instead of using a reference argument.
â Some programmer dude
5 hours ago
2
be warned that your intended operation cannot be performed exactly: the number0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.
â Walter
4 hours ago
4
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
2
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for thedouble
type. In short, there's nothing gained. Generally, for primitive types (int
,double
, etc.) don't pass by reference unless you need to modify the value inside the function.
â Some programmer dude
3 hours ago
 |Â
show 7 more comments
2
Looks ok to me but do note that the finalvalue
may have joke digits after the decimal 15th significant figure, e.g.0.265
is actually0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passingprecision
byconst
reference?
â Bathsheba
5 hours ago
3
Unrelated to your problem, but you don't need to passprecision
by constant reference. Just pass by value, or possibly constant value (i.e. plaindouble precision
orconst double precision
). I would also personally rather return the new value instead of using a reference argument.
â Some programmer dude
5 hours ago
2
be warned that your intended operation cannot be performed exactly: the number0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.
â Walter
4 hours ago
4
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
2
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for thedouble
type. In short, there's nothing gained. Generally, for primitive types (int
,double
, etc.) don't pass by reference unless you need to modify the value inside the function.
â Some programmer dude
3 hours ago
2
2
Looks ok to me but do note that the final
value
may have joke digits after the decimal 15th significant figure, e.g. 0.265
is actually 0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passing precision
by const
reference?â Bathsheba
5 hours ago
Looks ok to me but do note that the final
value
may have joke digits after the decimal 15th significant figure, e.g. 0.265
is actually 0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passing precision
by const
reference?â Bathsheba
5 hours ago
3
3
Unrelated to your problem, but you don't need to pass
precision
by constant reference. Just pass by value, or possibly constant value (i.e. plain double precision
or const double precision
). I would also personally rather return the new value instead of using a reference argument.â Some programmer dude
5 hours ago
Unrelated to your problem, but you don't need to pass
precision
by constant reference. Just pass by value, or possibly constant value (i.e. plain double precision
or const double precision
). I would also personally rather return the new value instead of using a reference argument.â Some programmer dude
5 hours ago
2
2
be warned that your intended operation cannot be performed exactly: the number
0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.â Walter
4 hours ago
be warned that your intended operation cannot be performed exactly: the number
0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.â Walter
4 hours ago
4
4
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
2
2
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for the
double
type. In short, there's nothing gained. Generally, for primitive types (int
, double
, etc.) don't pass by reference unless you need to modify the value inside the function.â Some programmer dude
3 hours ago
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for the
double
type. In short, there's nothing gained. Generally, for primitive types (int
, double
, etc.) don't pass by reference unless you need to modify the value inside the function.â Some programmer dude
3 hours ago
 |Â
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
9
down vote
You can use std::trunc
:
return std::trunc(value * precision) / precision;
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
You can use std::trunc
:
return std::trunc(value * precision) / precision;
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
add a comment |Â
up vote
9
down vote
You can use std::trunc
:
return std::trunc(value * precision) / precision;
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
add a comment |Â
up vote
9
down vote
up vote
9
down vote
You can use std::trunc
:
return std::trunc(value * precision) / precision;
You can use std::trunc
:
return std::trunc(value * precision) / precision;
edited 4 hours ago
ash108
1,2851318
1,2851318
answered 4 hours ago
user2079303
73.5k553111
73.5k553111
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
add a comment |Â
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
@user463035818 it return nearest integer not greater in magnitude to the argument i.e. rounds towards zero.
â user2079303
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
well reading till the end of the sentence helps sometimes ;)
â user463035818
4 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53062846%2fbest-way-to-approximate-in-direction-to-zero-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Looks ok to me but do note that the final
value
may have joke digits after the decimal 15th significant figure, e.g.0.265
is actually0.26500000000000001332267629550187848508358001708984375
. Out of interest, why are you passingprecision
byconst
reference?â Bathsheba
5 hours ago
3
Unrelated to your problem, but you don't need to pass
precision
by constant reference. Just pass by value, or possibly constant value (i.e. plaindouble precision
orconst double precision
). I would also personally rather return the new value instead of using a reference argument.â Some programmer dude
5 hours ago
2
be warned that your intended operation cannot be performed exactly: the number
0.265
, for example, has no finite floating-point representation, so you must not rely in your code on any exactness.â Walter
4 hours ago
4
@Bathsheba "joke digits" ðÂÂÂ
â Lightness Races in Orbit
4 hours ago
2
Considering that references usually is implemented as pointers, you're still passing 64 bits of data, which also happens to happens to be the common size for the
double
type. In short, there's nothing gained. Generally, for primitive types (int
,double
, etc.) don't pass by reference unless you need to modify the value inside the function.â Some programmer dude
3 hours ago