Skip to content
Advertisement

Tag: floating-point

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. Answer Assuming that the exact values

Generating a random double in an exclusive range

I’m trying to generate a random floating point number between but not including it’s lower and upper bounds (lower, upper). I’ve seen a lot of questions about generating a number from, and including it’s lower bound up to, but not including it’s upper bound [lower, upper), but that’s not what I’m after. I’ve come up with two “solutions” to the

C compatible printf output for Java

I’d want to convert float/double to string, in Java and C, such that the outputs are both consistent and user friendly. By “user friendly”, I mean the string should be human readable and sound: a maximum number of significant digits, and some automatic switching to scientific notation when appropiate (the double could span all the valid range). By “consistent” I

How can I handle precision error with float in Java?

I’m wondering what the best way to fix precision errors is in Java. As you can see in the following example, there are precision errors: The result displayed is: loop value: 11 20.789999 loop value: 22 41.579998 loop value: 44 83.159996 loop value: 88 166.31999 loop value: 176 332.63998 loop value: 352 665.27997 loop value: 704 1330.5599 Also, if someone

How to get the decimal part of a float?

I need to extract the decimal part of a float number, but I get weird results: Why does this happen? Why do I get those values instead of 0.65? Answer float only has a few digit of precision so you should expect to see a round error fairly easily. try double this has more accuracy but still has rounding errors.

Prevent round off in String.format(“%.2f”, doubleValue) in Java

How do I prevent String.format(“%.2f”, doubleValue); from rounding off (round half up algorithm) instead of just truncating it? e.g. after formatting, I just want to discard the last digit, I know there are other ways to do this, I just want to know if this is possible using the String.format. Answer You can always set the rounding mode: http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html and

When should I use the “strictfp” keyword in java?

I’ve looked up what this does, but does anyone actually have an example of when you would use the strictfp keyword in Java? Has anyone actually found a use for this? Would there be any side-effects of just putting it on all my floating point operations? Answer Strictfp ensures that you get exactly the same results from your floating point

Advertisement