From: Avishay Zilka on 20 May 2010 02:25 Hi, this is my first message here, be nice :) I've read here some posts about rounding issues, but didn't find the exact thing I'm looking for: I need to write a function that rounds a number to have d digits in its mantisaa. For example: if d=3, then: 123.4 --> 123, 1.567 --> 1.57, -45.81 --> -45.8 I know how to round to d digits after the floating point, but this doesn't work on 123.4 for example, as I need to cut the whole floating part there... Please help :) Thanks! Avishay
From: Greg Heath on 20 May 2010 07:00 On May 20, 2:25 am, "Avishay Zilka" <avis...(a)gmail.com> wrote: > Hi, > this is my first message here, be nice :) > > I've read here some posts about rounding issues, but didn't find the exact thing I'm looking for: > I need to write a function that rounds a number to have d digits in its mantisaa. > For example: > if d=3, then: 123.4 --> 123, 1.567 --> 1.57, -45.81 --> -45.8 > I know how to round to d digits after the floating point, but this doesn't work on 123.4 for example, as I need to cut the whole floating part there.... > > Please help :) a = [123.4 1.567 -45.81] b = 10.^(2-floor(log10(abs(a)))) c = sign(a).*round(abs(a).*b)./b Hope this helps. Greg
From: Walter Roberson on 20 May 2010 10:25 Avishay Zilka wrote: > I need to write a function that rounds a number to have d digits in its > mantisaa. > For example: > if d=3, then: 123.4 --> 123, 1.567 --> 1.57, -45.81 --> -45.8 > I know how to round to d digits after the floating point, but this > doesn't work on 123.4 for example, as I need to cut the whole floating > part there... Sorry, rounding 1.567 to 1.57 is not possible in any current Matlab version, not unless you are using the Fixed-point toolbox or the Symbolic Toolbox. The problem is that 1.57 is not exactly representable in finite binary floating point arithmetic, similar to the same way that Pi/2 is not exactly representable in finite decimal arithmetic.
|
Pages: 1 Prev: Contour Plot Next: classregtree -- Can X be categorical variable(s) ? |