Skip to main content
Became Hot Network Question
added 267 characters in body
Source Link
matheorem
  • 17.7k
  • 8
  • 53
  • 121

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?

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]]] 

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

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?

Source Link
matheorem
  • 17.7k
  • 8
  • 53
  • 121

Efficient Round edition with different rounding direction

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]]] 

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