Bill
Thanks, I thought the cast would have told the compiler to reserve a storage space for a double. I suppose because I added the .0 to each of the values it know that they were floats/doubles?
However if I use the following it also gives an incorrect result.
Unsigned long long test = 0;
Test = (2482884 * 1024);
Printf("%llu\n",test);
I get 18446744071957057536 that is not wrapping but it certainly gives me the wrong answer?? Why would that happen?
Chris...
______________________________________________________
Please consider the environment before printing this email.
This message, including any attached documents, is intended for the addressees only. It may contain information that is confidential, privileged and/or exempt from disclosure. No rights to privilege or confidentiality have been waived. Any unauthorized use or disclosure is prohibited. If you have received this message in error, please reply to the sender by e-mail and delete or destroy all copies of this message.
______________________________________________________
Avant d'imprimer ce courriel, pensez à l'environnement.
Ce message, incluant tous les documents joints, est à l'intention des destinataires visés seulement. Il peut contenir des renseignements confidentiels, protégés et/ou ne pouvant pas être divulgués. Aucune renonciation n'est faite quant à sa nature confidentielle et privilégiée. Par conséquent, toute diffusion ou utilisation non autorisée est strictement interdite. Si vous avez reçu ce message par erreur, veuillez en aviser immédiatement l'expéditeur par retour de courriel et en détruire toutes les copies existantes.
-----Original Message-----
From: c400-l-bounces+chrish=shieldadvanced.ca@xxxxxxxxxxxx [mailto:c400-l-bounces+chrish=shieldadvanced.ca@xxxxxxxxxxxx] On Behalf Of Bill Seurer
Sent: June-01-11 12:24 PM
To: C programming iSeries / AS400
Subject: Re: [C400-L] Conversion to double.
c400-l-bounces+seurer=us.ibm.com@xxxxxxxxxxxx wrote on 06/01/2011
c400-l-bounces+11:17:23
AM:
Printf(“%f\n”,(double)(2482884 * 1024)); shows -1752494080.000000
Printf(“%f\n”,(2482884.0 * 1024.0)); shows 2542473216.000000
Printf(“%f\n”,((double)2482884 * (double)1024)); shows
2542473216.000000
It seems like the conversion to double does not work in the first
instance.
Am I doing something wrong? Shouldn’t the cast of the result make the
number correct?
2482884 * 1024 is 2,542,473,216 or in hex 0x978B1000. That is larger than the largest positive value that can fit in a signed integer (0x7FFFFFFF) so the value "wraps" and becomes negative.
-Bill
--
This is the C programming iSeries / AS400 (C400-L) mailing list To post a message email: C400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
http://archive.midrange.com/c400-l.
As an Amazon Associate we earn from qualifying purchases.