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

If you attempt to grapple an opponent that you are hidden from, do they roll at disadvantage?

Curses work by shouting - How to avoid collateral damage?

Coordinate position not precise

Greatest common substring

Applicability of Single Responsibility Principle

Do there exist finite commutative rings with identity that are not Bézout rings?

Why are on-board computers allowed to change controls without notifying the pilots?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Understanding "audieritis" in Psalm 94

What to do with wrong results in talks?

Teaching indefinite integrals that require special-casing

Should my PhD thesis be submitted under my legal name?

The plural of 'stomach"

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?

Can I use my Chinese passport to enter China after I acquired another citizenship?

How does residential electricity work?

What would be the benefits of having both a state and local currencies?

Is expanding the research of a group into machine learning as a PhD student risky?

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

Trouble understanding overseas colleagues

(Bedrock Edition) Loading more than six chunks at once

Dot above capital letter not centred

Can a monster with multiattack use this ability if they are missing a limb?

Can I Retrieve Email Addresses from BCC?



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
    Mar 17 at 11:58










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    Mar 17 at 12:02






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    Mar 17 at 12:25






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    Mar 17 at 13:58






  • 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
    Mar 17 at 18:36















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
    Mar 17 at 11:58










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    Mar 17 at 12:02






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    Mar 17 at 12:25






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    Mar 17 at 13:58






  • 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
    Mar 17 at 18:36













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 Mar 17 at 11:59







matheorem

















asked Mar 17 at 11:54









matheoremmatheorem

6,65743178




6,65743178











  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    Mar 17 at 11:58










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    Mar 17 at 12:02






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    Mar 17 at 12:25






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    Mar 17 at 13:58






  • 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
    Mar 17 at 18:36
















  • $begingroup$
    Floor[x+0.5]?
    $endgroup$
    – Szabolcs
    Mar 17 at 11:58










  • $begingroup$
    @Szabolcs But Floor gives integer. Round can round at any digit
    $endgroup$
    – matheorem
    Mar 17 at 12:02






  • 2




    $begingroup$
    Are your aware of RoundingRule?
    $endgroup$
    – Silvia
    Mar 17 at 12:25






  • 1




    $begingroup$
    Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
    $endgroup$
    – Daniel Lichtblau
    Mar 17 at 13:58






  • 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
    Mar 17 at 18:36















$begingroup$
Floor[x+0.5]?
$endgroup$
– Szabolcs
Mar 17 at 11:58




$begingroup$
Floor[x+0.5]?
$endgroup$
– Szabolcs
Mar 17 at 11:58












$begingroup$
@Szabolcs But Floor gives integer. Round can round at any digit
$endgroup$
– matheorem
Mar 17 at 12:02




$begingroup$
@Szabolcs But Floor gives integer. Round can round at any digit
$endgroup$
– matheorem
Mar 17 at 12:02




2




2




$begingroup$
Are your aware of RoundingRule?
$endgroup$
– Silvia
Mar 17 at 12:25




$begingroup$
Are your aware of RoundingRule?
$endgroup$
– Silvia
Mar 17 at 12:25




1




1




$begingroup$
Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
$endgroup$
– Daniel Lichtblau
Mar 17 at 13:58




$begingroup$
Could extend the method proposed by @Szabolcs: myRound[x_, d_] := d*Floor[x/d + 1/2]
$endgroup$
– Daniel Lichtblau
Mar 17 at 13:58




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
Mar 17 at 18:36




$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
Mar 17 at 18:36










1 Answer
1






active

oldest

votes


















3












$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
    Mar 17 at 13:22






  • 4




    $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
    Mar 17 at 15:14






  • 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
    Mar 17 at 15:40






  • 2




    $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
    Mar 17 at 18:38







  • 1




    $begingroup$
    @matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
    $endgroup$
    – Michael E2
    Mar 18 at 2:52










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









3












$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
    Mar 17 at 13:22






  • 4




    $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
    Mar 17 at 15:14






  • 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
    Mar 17 at 15:40






  • 2




    $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
    Mar 17 at 18:38







  • 1




    $begingroup$
    @matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
    $endgroup$
    – Michael E2
    Mar 18 at 2:52















3












$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
    Mar 17 at 13:22






  • 4




    $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
    Mar 17 at 15:14






  • 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
    Mar 17 at 15:40






  • 2




    $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
    Mar 17 at 18:38







  • 1




    $begingroup$
    @matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
    $endgroup$
    – Michael E2
    Mar 18 at 2:52













3












3








3





$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 Mar 17 at 12:33









mikadomikado

6,7471929




6,7471929











  • $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
    Mar 17 at 13:22






  • 4




    $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
    Mar 17 at 15:14






  • 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
    Mar 17 at 15:40






  • 2




    $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
    Mar 17 at 18:38







  • 1




    $begingroup$
    @matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
    $endgroup$
    – Michael E2
    Mar 18 at 2:52
















  • $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
    Mar 17 at 13:22






  • 4




    $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
    Mar 17 at 15:14






  • 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
    Mar 17 at 15:40






  • 2




    $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
    Mar 17 at 18:38







  • 1




    $begingroup$
    @matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
    $endgroup$
    – Michael E2
    Mar 18 at 2:52















$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
Mar 17 at 13:22




$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
Mar 17 at 13:22




4




4




$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
Mar 17 at 15:14




$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
Mar 17 at 15:14




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
Mar 17 at 15:40




$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
Mar 17 at 15:40




2




2




$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
Mar 17 at 18:38





$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
Mar 17 at 18:38





1




1




$begingroup$
@matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
$endgroup$
– Michael E2
Mar 18 at 2:52




$begingroup$
@matheorem That's what happens (automatically) when you use real (floating-point) numbers on all common CPUs.
$endgroup$
– Michael E2
Mar 18 at 2:52

















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

Solar Wings Breeze Design and development Specifications (Breeze) References Navigation menu1368-485X"Hang glider: Breeze (Solar Wings)"e

Kathakali Contents Etymology and nomenclature History Repertoire Songs and musical instruments Traditional plays Styles: Sampradayam Training centers and awards Relationship to other dance forms See also Notes References External links Navigation menueThe Illustrated Encyclopedia of Hinduism: A-MSouth Asian Folklore: An EncyclopediaRoutledge International Encyclopedia of Women: Global Women's Issues and KnowledgeKathakali Dance-drama: Where Gods and Demons Come to PlayKathakali Dance-drama: Where Gods and Demons Come to PlayKathakali Dance-drama: Where Gods and Demons Come to Play10.1353/atj.2005.0004The Illustrated Encyclopedia of Hinduism: A-MEncyclopedia of HinduismKathakali Dance-drama: Where Gods and Demons Come to PlaySonic Liturgy: Ritual and Music in Hindu Tradition"The Mirror of Gesture"Kathakali Dance-drama: Where Gods and Demons Come to Play"Kathakali"Indian Theatre: Traditions of PerformanceIndian Theatre: Traditions of PerformanceIndian Theatre: Traditions of PerformanceIndian Theatre: Traditions of PerformanceMedieval Indian Literature: An AnthologyThe Oxford Companion to Indian TheatreSouth Asian Folklore: An Encyclopedia : Afghanistan, Bangladesh, India, Nepal, Pakistan, Sri LankaThe Rise of Performance Studies: Rethinking Richard Schechner's Broad SpectrumIndian Theatre: Traditions of PerformanceModern Asian Theatre and Performance 1900-2000Critical Theory and PerformanceBetween Theater and AnthropologyKathakali603847011Indian Theatre: Traditions of PerformanceIndian Theatre: Traditions of PerformanceIndian Theatre: Traditions of PerformanceBetween Theater and AnthropologyBetween Theater and AnthropologyNambeesan Smaraka AwardsArchivedThe Cambridge Guide to TheatreRoutledge International Encyclopedia of Women: Global Women's Issues and KnowledgeThe Garland Encyclopedia of World Music: South Asia : the Indian subcontinentThe Ethos of Noh: Actors and Their Art10.2307/1145740By Means of Performance: Intercultural Studies of Theatre and Ritual10.1017/s204912550000100xReconceiving the Renaissance: A Critical ReaderPerformance TheoryListening to Theatre: The Aural Dimension of Beijing Opera10.2307/1146013Kathakali: The Art of the Non-WorldlyOn KathakaliKathakali, the dance theatreThe Kathakali Complex: Performance & StructureKathakali Dance-Drama: Where Gods and Demons Come to Play10.1093/obo/9780195399318-0071Drama and Ritual of Early Hinduism"In the Shadow of Hollywood Orientalism: Authentic East Indian Dancing"10.1080/08949460490274013Sanskrit Play Production in Ancient IndiaIndian Music: History and StructureBharata, the Nāṭyaśāstra233639306Table of Contents2238067286469807Dance In Indian Painting10.2307/32047833204783Kathakali Dance-Theatre: A Visual Narrative of Sacred Indian MimeIndian Classical Dance: The Renaissance and BeyondKathakali: an indigenous art-form of Keralaeee

Method to test if a number is a perfect power? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Detecting perfect squares faster than by extracting square rooteffective way to get the integer sequence A181392 from oeisA rarely mentioned fact about perfect powersHow many numbers such $n$ are there that $n<100,lfloorsqrtn rfloor mid n$Check perfect squareness by modulo division against multiple basesFor what pair of integers $(a,b)$ is $3^a + 7^b$ a perfect square.Do there exist any positive integers $n$ such that $lfloore^nrfloor$ is a perfect power? What is the probability that one exists?finding perfect power factors of an integerProve that the sequence contains a perfect square for any natural number $m $ in the domain of $f$ .Counting Perfect Powers