public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: henri.cloetens@blueice.be
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Amit Hiremath <amit.hmath@googlemail.com>, gcc-help@gcc.gnu.org
Subject: Re: Decimal to floating point and floating point to decimal conversion
Date: Tue, 11 Jul 2023 14:33:46 +0200	[thread overview]
Message-ID: <cb7e59156243f27ed184a43e07a113bc@blueice.be> (raw)
In-Reply-To: <CAH6eHdRxTK4KU+WuaSs2ZzfwfWZr_Uhe8wxB2ZZwRz8OVSzkoQ@mail.gmail.com>

Hello Jonathan,

Can you please explain your point of view ?.

Note that, with any of the 2 codes, what is printed in hex
(with the %08x formatter, after converting to integer), is the hex value 
of
the representation in machine format of the floating-point number.
This is normally dependent on the machine representation of the 
floating-point
number. Normally, in this case, it should be 32-bit IEEE754 format.
(Look for it on Wikipedia, eg.)
If you look at the number : 0x404FA4F1 :
a. The MSB bit is the sign. Here it is 0 : positive
b. bit 30 to 23 are the exponent Here it is 0x80 or 128
c. bit 22 to 0 are the mantissa. Here it is 0x4FA4F1

... To convert it by hand: ...
linux>
bc -l
ibase = 16  (sets input to hex)
mant = 0x4FA4F1
dec_point = 0x800000  (the decimal point - cfr Wikipedia page)
exp = 80
ibase = A  (sets input back to decimal)
exp_bias = 127 (cfr Wikipedia page on IEEE754)

(mant + dec_point)/dec_point * e(l(2)*(exp-exp_bias))

... and there you have the other representation !.
Note :
(mant + dec_point) : in IEEE754 the most significant bit is omitted from 
the mantissa.
                      it is assumed to be 1.
e(l(2) * x) : method to calculate 2 ** x in bc.

Cheers,

Henri.


On 2023-07-11 11:44, Jonathan Wakely wrote:
> On Mon, 10 Jul 2023 at 19:23, <henri.cloetens@blueice.be> wrote:
>> 
>> Hello Amit,
>> 
>> If you want to do this conversion in C, proceed as follows :
>> 
>> #include "stdio.h"
>> #include "stdlib.h"
>> 
>> int main()
>> {
>> float a = 3.2444422 ;
>> int *p = (int *)(&a) ;
>> fprintf(stderr,"%08x\n",*p) ;
>> }
>> 
>> It will print the hex value, which happens to be the same as yours on 
>> my
>> machine.
> 
> That code has undefined behaviour.
> 
> This would be valid:
> 
> float a = 3.2444422 ;
> int i;
> memcpy(&i, &f, sizeof(int));
> fprintf(stderr,"%08x\n",i) ;

  reply	other threads:[~2023-07-11 12:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 10:27 Amit Hiremath
2023-07-10 11:42 ` Jonathan Wakely
2023-07-10 11:45   ` Jonathan Wakely
2023-07-10 12:59   ` Amit Hiremath
2023-07-10 13:13     ` Matthias Pfaller
2023-07-10 17:12       ` Richard Earnshaw
2023-07-10 18:22 ` henri.cloetens
2023-07-11  9:44   ` Jonathan Wakely
2023-07-11 12:33     ` henri.cloetens [this message]
2023-07-11 12:45       ` Jonathan Wakely
2023-07-11 12:47       ` Xi Ruoyao
2023-07-11 12:47       ` Andrew Haley
2023-07-12  9:16   ` Amit Hiremath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cb7e59156243f27ed184a43e07a113bc@blueice.be \
    --to=henri.cloetens@blueice.be \
    --cc=amit.hmath@googlemail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).