Mail Archives: djgpp/1996/11/21/05:44:01
Matt J Reiferson (msnakes AT juno DOT com) wrote:
: >int auks;
: >auks = 19.99 + 11.99;
: change your code to one of these:
: float auks;
: auks = 19.99 + 11.99;
: or
: int auks;
: auks = (float)(19.99 + 11.99);
Wrong. This just changes the warning to one about an implicit cast from
float to int. I believe you meant:
int auks;
auks = (int) (19.99 + 11.99);
This rounds down by default, I believe; IMHO it's better to first use
either floor or ceil to specify the rounding explicitly, i.e.:
int auks = (int) floor(19.99 + 11.99);
gives the above result (31), whereas
int auks = (int) ceil(19.99 + 11.99);
gives the lowest integer greater than or equal to the result, i.e. 32. If
you want to round it to the nearest (rounding halves up, i.e. 1.5 => 2),
use:
int auks = (int) floor(19.99 + 11.99 + 0.5);
which, of course, also gives 32. The typecasts to int are still required;
these functions return doubles. You also need to #include <math.h> before
using them.
I hope this clears up any misunderstanding.
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |""""""""""""""""""""""|
>Email: george DOT foot AT merton DOT ox DOT ac DOT uk < |Snail Mail:(*) |
> or: mert0407 AT sable DOT ox DOT ac DOT uk < | _ George Foot |
> Web: http://users.ox.ac.uk/~mert0407/ < |(@) Merton College |
> Ftp: mc31.merton.ox.ac.uk (#) < |~~~~ Oxford OX1 4JD |
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ |______________________|
(#) Theoretically... || (*) Please allow 28 days for delivery
- Raw text -