Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ceiling to Nearest 50

I can round the elements of A to the nearest integers greater than or equal to A

ceil(A)

But what about if I want to round it to the nearest 50 greater than or equal to A?

For example, given the following A array,

A=[24, 35, 78, 101, 199];

A subroutine should return the following

B=Subroutine(A)=[50, 50, 100, 150, 200];
like image 782
Graviton Avatar asked Oct 02 '10 02:10

Graviton


People also ask

How do I roundup to nearest .5 in Excel?

To round a number up to nearest 0.5, use the CEILING function, for example =CEILING(A2, 0.5) . To round a number up or down to nearest 0.5, use the MROUND function, e.g. =MROUND(A2, 0.5) .

How do you round to the nearest 100 in Excel?

Round Up or Round Down to Nearest 100 You can use the ROUNDUP or ROUNDDOWN Functions to round a number up or down instead of using standard rounding rules.

Does Excel round 0.5 up or down?

Excel has a very elementary take on rounding. It's the same method you learned early in grade school: If you're rounding to a whole number, decimals from 0.1 to 0.4 round down and decimals from 0.5 to 0.9 round up.


2 Answers

You could just divide by 50, take ceil(), and multiply by 50 again:

  octave:1> A=[24, 35, 78, 101, 199];
  octave:2> ceil(A)
  ans =

    24    35    78   101   199

  octave:3> 50*(ceil(A/50.))
  ans =

    50    50   100   150   200
like image 64
Jonathan Dursi Avatar answered Oct 19 '22 14:10

Jonathan Dursi


An easy way is to just add each number's complement modulo 50:

octave> A = [24, 35, 78, 101, 199] 

octave> mod(-A, 50)       # Complement (mod 50)
ans =

   26   15   22   49    1

octave> A + mod(-A, 50)   # Sum to "next higher" zero (mod 50)
ans =

    50    50   100   150   200

octave> A - mod(A, 50)    # Can also sum to "next lower" zero (mod 50)
ans =

     0     0    50   100   150

(Note that this only depends on integer arithmetic, which avoids errors due to floating-point rounding.)

like image 41
Pi Delport Avatar answered Oct 19 '22 13:10

Pi Delport