Over a million developers have joined DZone.

Exercise to understand double.

· Performance Zone

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.

Many developers fell like double' rounding and representation errors and random errors which are wild and uncontrollable.   This exercise is intended to help understand what double is really doing.



Write a method to convert double into text in a direct ByteBuffer which starts

public static void append(ByteBuffer bb, double d) {
    long val = Double.doubleToRawLongBits(d);
    int sign = (int) (val >>> 63);
    int exp = (int) ((val >>> 52) & 2047);
    long mantissa = val & ((1L << 52) - 1);


The goal is to write +/- 0.0001 to 100000.0 using no objects or double, just integer arithmetic.  Values outside this range can use BigDecimal or Double as a fall back.

  • Implement this to convert to text the actual representation of the double, just as new BigDecimal(double) does. (Write a test which compares the values) e.g. 0.1d prints as 0.1000000000000000055511151231257827021181583404541015625
  • Implement this to convert to text the closest rounded value which would have this representation as a double. i.e. 0.1d prints as 0.1
  • In both cases, compare the performance with using BigDecimal and Double.toString copied to a ByteBuffer.
  • Determine what is the actual range you need to use BigDecimal or Double as a fall back. Note: there is no strictly correct answer to this.
Bonus Points: Write the routine to use Unsafe and compare the performance.

See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}