Java – iterates over each possible double value

Consider the situation where you want to test each possible input value Creating a situation that iterates over all possible integers is simple because you can increment the value by 1 and repeat

How would you do the same for all possible double values?

Solution

You can iterate over all possible long values and then use double Longbitstodouble() gets a double value for each possible 64 - bit combination

But please note that this will take some time If each double value requires 100 nanoseconds of processing, it will roughly require (not all bit combinations are different even numbers, such as Nan) 2 ^ 64 * 1e-7 / 86400 / 365 years, greater than 16e11 / 86400 / 365 = 50700 years on a single CPU Unless you have a data center to calculate, it is best to consider the possible range of all input values and sample the interval with configurable points

The similar expertise of floating is still difficult but feasible: assuming that you need to process each input value for 10 milliseconds, you need about 2 ^ 32 * 1e-2 / 86400 = 497.1 days on a single CPU In this case, you will use float intBitsToFloat().

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>