Efficient Round edition with different rounding directionWhy round to even integers?How to prevent Round with hided fractionsVery different results from evaluating same expression with different precisionsProblems with rounding giving too many digits

gerund and noun applications

Can you move over difficult terrain with only 5 feet of movement?

Does .bashrc contain syntax errors?

Matrix using tikz package

Is a party consisting of only a bard, a cleric, and a warlock functional long-term?

Turning a hard to access nut?

What does Deadpool mean by "left the house in that shirt"?

Do I need to consider instance restrictions when showing a language is in P?

Tikz: place node leftmost of two nodes of different widths

Asserting that Atheism and Theism are both faith based positions

What is the term when voters “dishonestly” choose something that they do not want to choose?

How can an organ that provides biological immortality be unable to regenerate?

Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?

Print a physical multiplication table

What can I do if I am asked to learn different programming languages very frequently?

Violin - Can double stops be played when the strings are not next to each other?

Brake pads destroying wheels

What does "^L" mean in C?

Relation between independence and correlation of uniform random variables

Do US professors/group leaders only get a salary, but no group budget?

Optimising a list searching algorithm

Can other pieces capture a threatening piece and prevent a checkmate?

Pronounciation of the combination "st" in spanish accents

Is this an example of a Neapolitan chord?



Efficient Round edition with different rounding direction


Why round to even integers?How to prevent Round with hided fractionsVery different results from evaluating same expression with different precisionsProblems with rounding giving too many digits













1












$begingroup$


As pointed out in this post, Mathematica has a special version of Round that




Round rounds numbers of the form x.5 toward the nearest even integer.




A comment by David G suggest that why not have differnt options Direction → "HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"



These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below



myRound[x_, d_] := Module[,
c1 = (1./d)*10;
c2 = 1./d;
theDigit = Last@IntegerDigits@IntegerPart[x*c1];
If[theDigit >= 5,
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2],
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]]


speed test



In[267]:= 
myRound[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[267]= 30.7072, Null

In[268]:=
Round[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[268]= 0.285921, Null


So I am wondering if someone on this site already have developed an efficient toolkit for round matters?










share|improve this question











$endgroup$











  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    12 hours ago










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    12 hours ago






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    12 hours ago






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    10 hours ago






  • 1




    $begingroup$
    That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
    $endgroup$
    – Daniel Lichtblau
    6 hours ago















1












$begingroup$


As pointed out in this post, Mathematica has a special version of Round that




Round rounds numbers of the form x.5 toward the nearest even integer.




A comment by David G suggest that why not have differnt options Direction → "HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"



These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below



myRound[x_, d_] := Module[,
c1 = (1./d)*10;
c2 = 1./d;
theDigit = Last@IntegerDigits@IntegerPart[x*c1];
If[theDigit >= 5,
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2],
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]]


speed test



In[267]:= 
myRound[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[267]= 30.7072, Null

In[268]:=
Round[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[268]= 0.285921, Null


So I am wondering if someone on this site already have developed an efficient toolkit for round matters?










share|improve this question











$endgroup$











  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    12 hours ago










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    12 hours ago






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    12 hours ago






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    10 hours ago






  • 1




    $begingroup$
    That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
    $endgroup$
    – Daniel Lichtblau
    6 hours ago













1












1








1





$begingroup$


As pointed out in this post, Mathematica has a special version of Round that




Round rounds numbers of the form x.5 toward the nearest even integer.




A comment by David G suggest that why not have differnt options Direction → "HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"



These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below



myRound[x_, d_] := Module[,
c1 = (1./d)*10;
c2 = 1./d;
theDigit = Last@IntegerDigits@IntegerPart[x*c1];
If[theDigit >= 5,
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2],
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]]


speed test



In[267]:= 
myRound[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[267]= 30.7072, Null

In[268]:=
Round[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[268]= 0.285921, Null


So I am wondering if someone on this site already have developed an efficient toolkit for round matters?










share|improve this question











$endgroup$




As pointed out in this post, Mathematica has a special version of Round that




Round rounds numbers of the form x.5 toward the nearest even integer.




A comment by David G suggest that why not have differnt options Direction → "HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"



These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below



myRound[x_, d_] := Module[,
c1 = (1./d)*10;
c2 = 1./d;
theDigit = Last@IntegerDigits@IntegerPart[x*c1];
If[theDigit >= 5,
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2],
Internal`StringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]]


speed test



In[267]:= 
myRound[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[267]= 30.7072, Null

In[268]:=
Round[#, 0.01] & /@ RandomReal[1., 1000000]; // AbsoluteTiming

Out[268]= 0.285921, Null


So I am wondering if someone on this site already have developed an efficient toolkit for round matters?







machine-precision precision-and-accuracy round






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago







matheorem

















asked 13 hours ago









matheoremmatheorem

6,65743178




6,65743178











  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    12 hours ago










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    12 hours ago






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    12 hours ago






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    10 hours ago






  • 1




    $begingroup$
    That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
    $endgroup$
    – Daniel Lichtblau
    6 hours ago
















  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    12 hours ago










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    12 hours ago






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    12 hours ago






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    10 hours ago






  • 1




    $begingroup$
    That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
    $endgroup$
    – Daniel Lichtblau
    6 hours ago















$begingroup$
Floor[x+0.5]?
$endgroup$
– Szabolcs
12 hours ago




$begingroup$
Floor[x+0.5]?
$endgroup$
– Szabolcs
12 hours ago












$begingroup$
@Szabolcs But Floor gives integer. Round can round at any digit
$endgroup$
– matheorem
12 hours ago




$begingroup$
@Szabolcs But Floor gives integer. Round can round at any digit
$endgroup$
– matheorem
12 hours ago




2




2




$begingroup$
Are your aware of RoundingRule?
$endgroup$
– Silvia
12 hours ago




$begingroup$
Are your aware of RoundingRule?
$endgroup$
– Silvia
12 hours ago




1




1




$begingroup$
Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
$endgroup$
– Daniel Lichtblau
10 hours ago




$begingroup$
Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
$endgroup$
– Daniel Lichtblau
10 hours ago




1




1




$begingroup$
That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
$endgroup$
– Daniel Lichtblau
6 hours ago




$begingroup$
That's a result of using decimal values e.g. .01 that do not have exact binary equivalents. Could instead do myRound[8.121,1/100] and numericize afterward.
$endgroup$
– Daniel Lichtblau
6 hours ago










1 Answer
1






active

oldest

votes


















2












$begingroup$

I offer the following solution



r2[x_, a_] := x - Mod[x, a, -(a/2)]


We can verify that it has the desired result, using PiecewiseExpand



PiecewiseExpand[r2[x, a], -2 a < x < 2 a && a > 0]


Performance is only a little slower than the built-in Round



list = RandomReal[0, 1, 1000000];

AbsoluteTiming[Round[list, 0.1];]
(* 0.0079, Null *)

AbsoluteTiming[r2[list, 0.1];]
(* 0.009414, Null *)





share|improve this answer









$endgroup$












  • $begingroup$
    Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
    $endgroup$
    – matheorem
    11 hours ago










  • $begingroup$
    I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
    $endgroup$
    – matheorem
    11 hours ago






  • 3




    $begingroup$
    @matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
    $endgroup$
    – Michael E2
    9 hours ago






  • 2




    $begingroup$
    @matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
    $endgroup$
    – Michael E2
    9 hours ago






  • 1




    $begingroup$
    @matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
    $endgroup$
    – mikado
    6 hours ago











Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193412%2fefficient-round-edition-with-different-rounding-direction%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2












$begingroup$

I offer the following solution



r2[x_, a_] := x - Mod[x, a, -(a/2)]


We can verify that it has the desired result, using PiecewiseExpand



PiecewiseExpand[r2[x, a], -2 a < x < 2 a && a > 0]


Performance is only a little slower than the built-in Round



list = RandomReal[0, 1, 1000000];

AbsoluteTiming[Round[list, 0.1];]
(* 0.0079, Null *)

AbsoluteTiming[r2[list, 0.1];]
(* 0.009414, Null *)





share|improve this answer









$endgroup$












  • $begingroup$
    Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
    $endgroup$
    – matheorem
    11 hours ago










  • $begingroup$
    I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
    $endgroup$
    – matheorem
    11 hours ago






  • 3




    $begingroup$
    @matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
    $endgroup$
    – Michael E2
    9 hours ago






  • 2




    $begingroup$
    @matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
    $endgroup$
    – Michael E2
    9 hours ago






  • 1




    $begingroup$
    @matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
    $endgroup$
    – mikado
    6 hours ago
















2












$begingroup$

I offer the following solution



r2[x_, a_] := x - Mod[x, a, -(a/2)]


We can verify that it has the desired result, using PiecewiseExpand



PiecewiseExpand[r2[x, a], -2 a < x < 2 a && a > 0]


Performance is only a little slower than the built-in Round



list = RandomReal[0, 1, 1000000];

AbsoluteTiming[Round[list, 0.1];]
(* 0.0079, Null *)

AbsoluteTiming[r2[list, 0.1];]
(* 0.009414, Null *)





share|improve this answer









$endgroup$












  • $begingroup$
    Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
    $endgroup$
    – matheorem
    11 hours ago










  • $begingroup$
    I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
    $endgroup$
    – matheorem
    11 hours ago






  • 3




    $begingroup$
    @matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
    $endgroup$
    – Michael E2
    9 hours ago






  • 2




    $begingroup$
    @matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
    $endgroup$
    – Michael E2
    9 hours ago






  • 1




    $begingroup$
    @matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
    $endgroup$
    – mikado
    6 hours ago














2












2








2





$begingroup$

I offer the following solution



r2[x_, a_] := x - Mod[x, a, -(a/2)]


We can verify that it has the desired result, using PiecewiseExpand



PiecewiseExpand[r2[x, a], -2 a < x < 2 a && a > 0]


Performance is only a little slower than the built-in Round



list = RandomReal[0, 1, 1000000];

AbsoluteTiming[Round[list, 0.1];]
(* 0.0079, Null *)

AbsoluteTiming[r2[list, 0.1];]
(* 0.009414, Null *)





share|improve this answer









$endgroup$



I offer the following solution



r2[x_, a_] := x - Mod[x, a, -(a/2)]


We can verify that it has the desired result, using PiecewiseExpand



PiecewiseExpand[r2[x, a], -2 a < x < 2 a && a > 0]


Performance is only a little slower than the built-in Round



list = RandomReal[0, 1, 1000000];

AbsoluteTiming[Round[list, 0.1];]
(* 0.0079, Null *)

AbsoluteTiming[r2[list, 0.1];]
(* 0.009414, Null *)






share|improve this answer












share|improve this answer



share|improve this answer










answered 12 hours ago









mikadomikado

6,6671929




6,6671929











  • $begingroup$
    Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
    $endgroup$
    – matheorem
    11 hours ago










  • $begingroup$
    I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
    $endgroup$
    – matheorem
    11 hours ago






  • 3




    $begingroup$
    @matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
    $endgroup$
    – Michael E2
    9 hours ago






  • 2




    $begingroup$
    @matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
    $endgroup$
    – Michael E2
    9 hours ago






  • 1




    $begingroup$
    @matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
    $endgroup$
    – mikado
    6 hours ago

















  • $begingroup$
    Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
    $endgroup$
    – matheorem
    11 hours ago










  • $begingroup$
    I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
    $endgroup$
    – matheorem
    11 hours ago






  • 3




    $begingroup$
    @matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
    $endgroup$
    – Michael E2
    9 hours ago






  • 2




    $begingroup$
    @matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
    $endgroup$
    – Michael E2
    9 hours ago






  • 1




    $begingroup$
    @matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
    $endgroup$
    – mikado
    6 hours ago
















$begingroup$
Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
$endgroup$
– matheorem
11 hours ago




$begingroup$
Wow, great solution. But there is a issue with hidden fractions as pointed out by mathematica.stackexchange.com/q/65298/4742 . But if we use InternalStringToDouble@ToString`, the performance will drop down.
$endgroup$
– matheorem
11 hours ago












$begingroup$
I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
$endgroup$
– matheorem
11 hours ago




$begingroup$
I figured out a trick, though I don't know why it works. The efficient solution to hidden fraction may be r2[x_, a_] := (IntegerPart[(x/a - Mod[x/a, 1, -(1/2)])]*10^12)/ 10.^(12 - 1/Log[a, 10])
$endgroup$
– matheorem
11 hours ago




3




3




$begingroup$
@matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
$endgroup$
– Michael E2
9 hours ago




$begingroup$
@matheorem Technically, 1.265 in binary floating point corresponds to the fraction 5697053528623677/4503599627370496 which less than 1265/1000. The issue is due to rounding decimal to binary. Thus it is a problem of inputting the number you meant, not a problem with r2[].
$endgroup$
– Michael E2
9 hours ago




2




2




$begingroup$
@matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
$endgroup$
– Michael E2
9 hours ago




$begingroup$
@matheorem The problem with imprecise calculation is that unwanted errors creep in, such as what you're complaining about. :) How about this?: r2[x_, a_] := x (1 + Sign[x] $MachineEpsilon) - Mod[x (1 + Sign[x] $MachineEpsilon), a, -(a/2)]
$endgroup$
– Michael E2
9 hours ago




1




1




$begingroup$
@matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
$endgroup$
– mikado
6 hours ago





$begingroup$
@matheorem, I think you need to consider why you know the input is exactly 1.265 rather than a little more or a little less, and why the rounding is important to you. If you know the 3rd decimal place is exact, I suggest you multiply all your numbers by 1000, and work with integers.
$endgroup$
– mikado
6 hours ago


















draft saved

draft discarded
















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193412%2fefficient-round-edition-with-different-rounding-direction%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

Чепеларе Съдържание География | История | Население | Спортни и природни забележителности | Културни и исторически обекти | Религии | Обществени институции | Известни личности | Редовни събития | Галерия | Източници | Литература | Външни препратки | Навигация41°43′23.99″ с. ш. 24°41′09.99″ и. д. / 41.723333° с. ш. 24.686111° и. д.*ЧепелареЧепеларски Linux fest 2002Начало на Зимен сезон 2005/06Национални хайдушки празници „Капитан Петко Войвода“Град ЧепелареЧепеларе – народният ски курортbgrod.orgwww.terranatura.hit.bgСправка за населението на гр. Исперих, общ. Исперих, обл. РазградМузей на родопския карстМузей на спорта и скитеЧепеларебългарскибългарскианглийскитукИстория на градаСки писти в ЧепелареВремето в ЧепелареРадио и телевизия в ЧепелареЧепеларе мами с родопски чар и добри пистиЕвтин туризъм и снежни атракции в ЧепелареМестоположениеИнформация и снимки от музея на родопския карст3D панорами от ЧепелареЧепелареррр