Round function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Round function

Rory Conolly
The round function in Octave rounds to the nearest integer while round in MATLAB supports rounding to arbitrary precision:

round(x,n) rounds x to n decimal places.

This is just a suggestion/request that it would be handy for the Octave function to work like the equivalent MATLAB function. This capability can be important when needing to compare numbers using == when  numerical noise can cause problems.

Sent from my iPhone

Reply | Threaded
Open this post in threaded view
|

Re: Round function

Juan Pablo Carbajal-2
This is actually an easy fix,
new_round = @(x, n=0) round (x * 10^n) * 10^(-n)

If you could send a patch to the patch tracker with:
1. extended documentation
2. new code
3. a few test cases

I would be happy to apply the patch and push it.

On Fri, Nov 16, 2018 at 2:41 PM Rory Conolly <[hidden email]> wrote:
>
> The round function in Octave rounds to the nearest integer while round in MATLAB supports rounding to arbitrary precision:
>
> round(x,n) rounds x to n decimal places.
>
> This is just a suggestion/request that it would be handy for the Octave function to work like the equivalent MATLAB function. This capability can be important when needing to compare numbers using == when  numerical noise can cause problems.
>
> Sent from my iPhone
>


Reply | Threaded
Open this post in threaded view
|

RE: Round function

Windhorn, Allen E [ACIM/LSA/MKT]
-----Original Message-----
From: Help-octave [mailto:help-octave-bounces+allen.windhorn=[hidden email]] On Behalf Of Juan Pablo Carbajal

> This is actually an easy fix,
> new_round = @(x, n=0) round (x * 10^n) * 10^(-n)

>> new_round(pi, 1.5)
ans =  3.1307

Maybe new_round = @(x, n=0) round (x * 10^round(n)) * 10^(-round(n))?
Depending on what you expect it to do.

Regards,
Allen