Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.development.apps > #452
| From | Bill M <wpmccormick@just_about_everywhere.com> |
|---|---|
| Newsgroups | comp.os.linux.development.apps |
| Subject | Re: cast double to short problem |
| Date | 2012-03-08 11:45 -0600 |
| Organization | A noiseless patient Spider |
| Message-ID | <jjar81$7q0$1@dont-email.me> (permalink) |
| References | <jj64g2$fb8$1@dont-email.me> <jj6650$fa0$1@reader1.panix.com> <alpine.DEB.2.00.1203071908210.1839@login01.caesar.elte.hu> <jj8fi1$ggn$1@reader1.panix.com> |
Grant Edwards wrote, On 3/7/2012 2:13 PM: > On 2012-03-07, Ersek, Laszlo<lacos@caesar.elte.hu> wrote: >> >>> Ah, there's your problem: expecting to get 3. You should expect to >>> get either 2 or 3. And you do. :) >> >> He should get the same result both times. > > Yes. He should. My point is that he shouldn't expect to. > > His code should not be written to depend on either (int)(0.3 * 10.0) > == 2 or on (int)(0.3 * 10.0) == 3. > >> - The floating point environment is the same. >> - dval starts out the same (as in "object representation"). >> - 10 (int) is converted to double the same way (with exact value). >> - Both double multiplications happen with the same operands (as in >> "object representation"). >> - Initialization of (~ simple assignment to) a double does not change the >> double product (as in "object representation"). >> - Conversion to short is the same (truncated toward zero). > > I agree: I think it should produce the same result in both cases. > Thanks for all the great replies!! For some silly reason I was thinking that the cast took care of the rounding. Even more silly, that my value was getting truncated had not occurred to me. In this situation, I don't need to be concerned with rounding up or down. I always want to round to the nearest decimal. The scenario is this: recipe_parms.dispense_volume1 = (int)round(atof(value) * 10); where dispense_volume1 has a precision of 0.1 or recipe_parms.suckback_volume = (int)round(atof(value) * 100); where suckback_volume has a precision of 0.01 So now the question of why are they different remains, but it does not affect my application. Thanks!!
Back to comp.os.linux.development.apps | Previous | Next — Previous in thread | Find similar
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