Best way to approximate in direction to zero in C++

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question

















  • 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







  • 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







  • 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














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.










share|improve this question

















  • 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







  • 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







  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









tony497

999




999







  • 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







  • 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







  • 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












  • 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







  • 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







  • 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







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












1 Answer
1






active

oldest

votes

















up vote
9
down vote













You can use std::trunc:



return std::trunc(value * precision) / precision;





share|improve this answer






















  • @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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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;





share|improve this answer






















  • @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














up vote
9
down vote













You can use std::trunc:



return std::trunc(value * precision) / precision;





share|improve this answer






















  • @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












up vote
9
down vote










up vote
9
down vote









You can use std::trunc:



return std::trunc(value * precision) / precision;





share|improve this answer














You can use std::trunc:



return std::trunc(value * precision) / precision;






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery