4

Everything I've found after searching has been how to use them. I'm really curious as to why. What are the benefits of using a floating point value in hex format?

Editing for clarification:

I am studying to get the Zend PHP Engineer certificate. In one of the practice tests, it posts a chunk of code with a few variables set, does some basic math, then asks what the output is. One of the variables set is "0xFF". Which as I understand it represents 255.0 as a floating point value.

So, my question is when is this

0xfed01

of better use than this

1043713
3
  • What do you mean? A floating-point value is just a series of bits, just as any other value in a computer. What are you trying to do? Feb 19, 2014 at 14:07
  • 1
    When you say that 0xFF represents 255.0, there probably is a conversion involved: it's the integer 0xFF converted to float. 0xFF is not the representation of 255.0 for any common format. C99 has an hexadecimal human-readable convention for printing and parsing floating-point numbers, but it would have to look like 0xFF.0p0 in order to be in that format (and that format has to my knowledge so far only made its way to Java). Long story short, are you sure that 0xFF is not actually an integer (later converted to float) and that your question isn't “Purpose of hexadecimal integer value?”? Feb 19, 2014 at 14:40
  • 1
    @PascalCuoq: (... only made its way to Java). And Python, too! Not as literals, but Python has float.hex and float.fromhex methods, so that e.g., 2.3.hex() gives '0x1.2666666666666p+1'. Feb 19, 2014 at 20:04

2 Answers 2

17

C has hexadecimal floating-point constants, with a form starting with “0x”, then hexadecimal digits, optionally including a radix point, then a “p”, then a signed exponent in decimal, which is a power of two. E.g., 0x1.fp-2 is (1 + 15/16)•2-2 = .484375. (These constants are double without a suffix, float with an f or F suffix, and long double with an l or L suffix.)

Hexadecimal floating-point constants make it easy for the compiler to reproduce the exact value.

With decimal constants, there is some difficulty in converting a decimal number to binary floating-point. This is because some values may be very close to a point where a value is either rounded up or down, depending on trailing digits. And it can be difficult to do the arithmetic for this, because numerical arithmetic is subject to rounding errors. Trying to avoid rounding errors while computing precise values involving digits beyond those normally represented by the floating-point format requires careful engineering.

This is a solved problem (Correctly Rounded Binary-Decimal and Decimal-Binary Conversions by David M. Gay, 1990). However, some compilers have not implemented it properly. It is easier for programmers to write code to convert hexadecimal floating-point because it fits the binary format nicely: You can easily tell whether a number is above or below a rounding point by examining a few individual digits; there is no need for involved arithmetic that compounds rounding errors.

Typically, a software engineer who is concerned about floating-point accuracy would prepare hexadecimal floating-point constants for use in a program by generating them with special software (e.g., Maple or Mathematica or their own custom task-specific software or a combination).

It is also possible to create a floating-point constant by specifying its internal representation, such as by creating a union of an unsigned integer and a floating-point object and initializing the union with a hexadecimal value for the integer. This requires knowledge of the encoding format of the floating-point value. It should rarely be done. Usually it is useful only for special purposes, such as preparing NaNs with payloads and work in software that does specialized floating-point operations.

4
  • 1
    Thank you very much, so it's for precise floating point comparisons. Feb 19, 2014 at 15:02
  • The Java spec (version 13) is a little unclear on whether hexadecimal floating-point literals are IEEE 754 32-bit single-precision or 64-bit double-precision. Do you know?
    – Urhixidur
    Oct 18, 2019 at 13:00
  • Hexadecimal floating-point literals are double-precision by default and may be "forced" to single-precision by the f or F suffix, just like decimal floating-point literals. (ref: dzone.com/articles/java-hexadecimal-floating-point-literal)
    – Urhixidur
    Oct 18, 2019 at 13:48
  • Note that the hex float format requires the p-affixed exponent, unlike decimal floats where the e-affixed exponent is optional. Thus, the trailing f is unambiguous (the exponent is always decimal).
    – o11c
    Jun 28, 2023 at 19:07
0

For more info on floating point conversions and their binary and hexadecimal layout you could take a look at crackNum

Installation on debian compatible systems is as easy as

apt-get install haskell-cracknum-utils

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.