Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why are hexadecimal float literals so goofy?

Reply
Thread Tools

Why are hexadecimal float literals so goofy?

 
 
Simon
Guest
Posts: n/a
 
      05-31-2011
I looked over the C99 spec for hexadecimal constants. They seem quite
odd on two accounts. For one, it is a binary exponent value, but the
other number is hexadecimal. Why not make them both hexadecimal? You
really don't lose anything...you just have to shift your radix point a
bit. This just seems confusing as it is.

The other is even stranger. The binary exponent value is actually
represented in decimal. What is decimal doing here? Isn't it
satisfied already being the only choice so far even though it
introduces round off errors and results in a less elegant compiler
implementation? Decimal is like a virus that decays elegance,
simplicity, and happiness.

Cheers
Simon
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-31-2011
Simon <> writes:

> I looked over the C99 spec for hexadecimal constants. They seem quite
> odd on two accounts. For one, it is a binary exponent value, but the
> other number is hexadecimal. Why not make them both hexadecimal? You
> really don't lose anything...you just have to shift your radix point a
> bit. This just seems confusing as it is.


I have always assumed that hexadecimal constants were written in
hexadecimal instead of binary just to save space. That is, the
underlying representation of floating-point numbers is assumed to
be in base 2, and one writes those in base 16 just to avoid
making the textual representation very long.

> The other is even stranger. The binary exponent value is actually
> represented in decimal. What is decimal doing here? Isn't it
> satisfied already being the only choice so far even though it
> introduces round off errors and results in a less elegant compiler
> implementation? Decimal is like a virus that decays elegance,
> simplicity, and happiness.


Representing the exponent in decimal doesn't introduce any
round-off errors, and it's easier for humans to read and
understand than representing it in binary or hexadecimal.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-31-2011
On 06/ 1/11 08:36 AM, Ben Pfaff wrote:
> Simon<> writes:
>
>> I looked over the C99 spec for hexadecimal constants. They seem quite
>> odd on two accounts. For one, it is a binary exponent value, but the
>> other number is hexadecimal. Why not make them both hexadecimal? You
>> really don't lose anything...you just have to shift your radix point a
>> bit. This just seems confusing as it is.

>
> I have always assumed that hexadecimal constants were written in
> hexadecimal instead of binary just to save space. That is, the
> underlying representation of floating-point numbers is assumed to
> be in base 2, and one writes those in base 16 just to avoid
> making the textual representation very long.


That is the logical conclusion. Although I am curious as to whether
anyone actually uses hexadecimal float literals!

--
Ian Collins
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-01-2011
On 05/31/2011 04:36 PM, Ben Pfaff wrote:
> Simon <> writes:
>
>> I looked over the C99 spec for hexadecimal constants. They seem quite
>> odd on two accounts. For one, it is a binary exponent value, but the
>> other number is hexadecimal. Why not make them both hexadecimal? You
>> really don't lose anything...you just have to shift your radix point a
>> bit. This just seems confusing as it is.

>
> I have always assumed that hexadecimal constants were written in
> hexadecimal instead of binary just to save space. That is, the
> underlying representation of floating-point numbers is assumed to
> be in base 2, and one writes those in base 16 just to avoid
> making the textual representation very long.


Yes, that is the main purpose, depending upon how you meant it.

A floating point number with N bits of precision can be exactly in
ceil(N/4.0) hexedecimal digits, but requires ceil(N*ln(2)/ln(10)) (+1?)
decimal digits to be expressed with sufficient precision to uniquely
determine what all N of those bits should be. That's only a 20% more
digits; it's not much of an motivation for using hexadecimal notation.

However, to express the number exactly would require N decimal digits -
that's the big advantage of hexadecimal floating point constants.

This advantage only applies when FLT_RADIX is a power of 2, but that's
by far the most common case. The most common alternative to FLT_RADIX==2
is FLT_RADIX==16, which provides the same advantage. I've also heard of
platforms where FLT_RADIX of 3 or 10 would be appropriate, but I don't
know if any implementation of C was ever created for such platforms.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Use hexadecimal literals for good style. Thinkit Java 10 01-02-2004 03:57 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57