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


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

cast double to short problem

From Bill M <wpmccormick@just_about_everywhere.com>
Newsgroups comp.os.linux.development.apps
Subject cast double to short problem
Date 2012-03-06 16:52 -0600
Organization A noiseless patient Spider
Message-ID <jj64g2$fb8$1@dont-email.me> (permalink)

Show all headers | View raw


I'm having a loss of precision problem when casting from a double to a 
short.

In the following, for dval = 0.3 ...

short sval = (short)(dval * 10);

... sval = 2. But if I convert it like this ...

double nval = dval * 10;
short sval = (short)nval;

... then sval = 3 as I would expect.

Can some gcc expert tell me what is happening here?

I'm not using any special options to gcc, but maybe that could be an issue?

Thanks!!

Back to comp.os.linux.development.apps | Previous | NextNext 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