Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

One day while trawling through the Java language documentation, as you do, I found this little beauty lurking within Double:

0.25 == 0x1.0p-2

Now, obviously (!) this means take the number hexadecimal 1 and right shift it decimal 2 times. The rule seems to be to use base 16 on the integer side and base 2 on the real side.

Has anyone out there actually used the right hand syntax in a necessary context, not just as a way getting beers out of your fellow developers?

share|improve this question
    
Why would you compare a constant with a constant anyway? –  Michael K Nov 16 '10 at 18:25
    
@Michael Edited to better illustrate the question –  Gary Rowe Nov 16 '10 at 18:30
add comment

2 Answers 2

up vote 6 down vote accepted

Just a guess: Some numbers that are rational in base10 are irrational in base2, and conversely some numbers that are rational in base2 are irrational in base10 (Please correct me if I'm wrong...I'm looking this up to confirm).

EDIT: thanks to "Note to self - think of a name" for correcting me.

I supposed that if you had a need to specify an exact binary value as a floating point (such as some epsilon value in graphical programming), then it might be more convenient to use this syntax. For example, 1/1024 is 0.0009765625 in base 10, but using this syntax it can be written as 0x1.0p-10

share|improve this answer
3  
I think you mean 'terminating', not 'rational'. Rational means a number can be represented by dividing integers, and terminating means there's a finite decimal representation. (roughly) Also, all numbers that terminate in base 2 also terminate in base 10, because 2 is a factor of 10, so that isn't a good explanation for the format. –  Note to self - think of a name Nov 16 '10 at 20:04
    
Yes, I think you're correct. I'm probably incorrectly remembering something about how some numbers that terminate in base 10 do not terminate in base 2, such as 1/10, but as you've pointed out, the converse is not true. Updating my answer... –  Dr. Wily's Apprentice Nov 16 '10 at 20:06
1  
How is 0x1.0p-10 more convenient than just 1/1024 ? :/ –  Peter Boughton Nov 17 '10 at 0:35
2  
If you're trying to debug floating point calculations, it may be convenient to be able to specify exact representations, down to the bit. The vast majority of Java programmers won't need to do this, but someone writing floating-point library routines might. –  Barry Brown Nov 17 '10 at 1:54
1  
@Peter, 1/1024 is a simple example, since we all know off the top of our head that 2^10 is 1024, but I'm sure there are better examples. I personally don't have every power of 2 up to 32 memorized, so how about 1/131072 as 0x1.0p-17? The point is that, perhaps for precision purposes, one may need to specify a value that is stored in a particular way as a floating point, and for that purpose using the hex syntax is probably easier since one doesn't have to think about what the decimal equivalent is. –  Dr. Wily's Apprentice Nov 17 '10 at 14:52
show 2 more comments

Completely insane, right? Many, many moons ago I worked with a guy that would write stuff like that. He came from an assembly background and would argue that his code would run faster than what the compiler would produce. I think this argument went away decades ago (along with programmers with an assembly background !)

share|improve this answer
1  
+1 because I'm pretty sure nobody can out-optimise (in source code) a modern compiler –  Gary Rowe Nov 16 '10 at 17:48
6  
@Gary: having done so (and seen others do so) I'm pretty sure they can -- but not with silliness like this. –  Jerry Coffin Nov 16 '10 at 18:43
    
@Jerry Coffin I'm impressed - please pass on my props to those involved. Which language was it? How did they verify it? –  Gary Rowe Nov 16 '10 at 20:48
    
@Gary: Java, C, C++, Pascal, Fortran, and a few others. Mostly by looking at the assembly language produced, but sometimes by timing the results. –  Jerry Coffin Nov 16 '10 at 21:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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