Java – calculates the nth floating point number

Using 32-bit floating-point numbers, I think there are floating-point numbers like 2 ^ 31 – 1 In Java, you can use an existing float and use the math library to find "next float" However, if you don't have a starting floating-point number, is there any way to calculate the nth floating-point number? I don't care what language, if there is a language with library functions, I will accept it

Of course, I can simply put all floating-point numbers into an array and index them, but it's not space efficient

These are some further clarifications I can get it from float Min starts and uses nextfloat n more times, but it seems inefficient because I need to do this many times

Solution

It depends on how you want to order Remember, not all floats are ordered; For example, a pair of different NANs are disordered (i.e., they are not equal, but neither is greater than the other)

If you don't mind those endings, you can reinterpret an integer as a floating point number The way you do this varies from language to language; This is a C implementation:

float int_to_float(uint32_t in) {
    union {
        float f;
        uint32_t i;
    } u;

    u.i = in;
    return u.f;
}

This has a convenient attribute that gives you most ordered results - passing in zero gets you 0.0, one gets you 1.4e-45, 2 gets you 2.8e-45, and so on Once you enter the Nan / inf value, the result will start to become crazy. Once you reach 0x80000000 (- 0.0), the result will eventually start to decrease, but it should be good enough now

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
分享
二维码
< <上一篇
下一篇>>