Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #449

Re: cast double to short problem

From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.os.linux.development.apps
Subject Re: cast double to short problem
Date 2012-03-07 15:23 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <jj7uhr$prg$1@reader1.panix.com> (permalink)
References <jj64g2$fb8$1@dont-email.me> <jj6650$fa0$1@reader1.panix.com> <jj6714$tbh$1@dont-email.me> <jj6a7s$l7k$1@reader1.panix.com> <jj7cii$ahl$1@dont-email.me>

Show all headers | View raw


On 2012-03-07, Noob <root@127.0.0.1> wrote:
> Grant Edwards wrote:
>
>> $ man round
>> #include <math.h>
>> i = (short) round(MyDoubleValue * 100.0);
>
> Depending on so-called rounding modes, some CPUs do not round as
> mathematicians expect.

IMO, depending on 1.5 or 2.5 being rounded one way or the other is a
bug, but I realize that in some statistical applications it matters.

However, the CPU type should have nothing to do with it -- the
behavior of the libm round() function is well defined to "round
halfway cases away from zero".  If you want half-way values to round
to nearest even then the round() man page says that you should call
rint().

> See, for example, the "nearest even" mode where both 1.5 and 2.5
> (which can be represented exactly in binary FP) are rounded to 2.
>
> http://www.ocf.berkeley.edu/~horie/rounding.html


-- 
Grant Edwards               grant.b.edwards        Yow! PARDON me, am I
                                  at               speaking ENGLISH?
                              gmail.com            

Back to comp.os.linux.development.apps | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

cast double to short problem Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-06 16:52 -0600
  Re: cast double to short problem Grant Edwards <invalid@invalid.invalid> - 2012-03-06 23:21 +0000
    Re: cast double to short problem Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-06 17:36 -0600
      Re: cast double to short problem Grant Edwards <invalid@invalid.invalid> - 2012-03-07 00:30 +0000
        Re: cast double to short problem Noob <root@127.0.0.1> - 2012-03-07 11:17 +0100
          Re: cast double to short problem Grant Edwards <invalid@invalid.invalid> - 2012-03-07 15:23 +0000
    Re: cast double to short problem "Ersek, Laszlo" <lacos@caesar.elte.hu> - 2012-03-07 19:33 +0100
      Re: cast double to short problem Grant Edwards <invalid@invalid.invalid> - 2012-03-07 20:13 +0000
        Re: cast double to short problem Bill M <wpmccormick@just_about_everywhere.com> - 2012-03-08 11:45 -0600

csiph-web