Skip to content

How can I intentionally create random floating point / decimal precision errors in java?

My goal is to generate random numbers that all have decimal precision errors.

Here are some examples of types of numbers I would like to generate:


Here are strategies I have tried.

parseFloat("4.01500000000000000001"); BigDecimal.valueOf(ThreadLocalRandom.current().nextFloat()).multiply(BigDecimal.valueOf(ThreadLocalRandom.current().nextFloat())).doubleValue();

None of the things I tried have created even a single number similar to that which I am looking for.


Assuming that the exact values actually meant are:

  • 1.81 repeating
  • 1.27 repeating
  • 0.4 repeating

Observe that in each number there are around 7 or 8 digits that are “correct”, and around 18 digits in total. floats are usually precise to 7 to 8 digits, and doubles are usually precise to around 18 digits, so I suspect that these numbers are actually floats (or 32-bit floating point numbers) that got “widened” to be doubles

1.81 repeating is 20/11, so I tried:

float i = 20f/11f;
double d = i;

This prints exactly the value you saw – 1.8181818723678589.

1.27 repeating is 14/11 and 0.4 repeating is 4/9. You can try both of these yourself. They all produce your desired numbers. This strongly suggests that the errors are produced by widening a float to 64 bits.