Java – how to convert a floating point number to the nearest fraction represented by a byte numerator and denominator?

How to write an algorithm that gives floating-point numbers and try to express them as accurately as possible using numerators and denominators, both of which are limited to the range of Java bytes?

The reason for this is that I2C devices need numerators and denominators, and it makes sense to give it a floating point number

For example, 3.1415926535... Will result in 245 / 78 instead of 314 / 100 or 22 / 7

In terms of efficiency, this will be called three times at the beginning of the program, but not at all after So the slow algorithm is not too bad

Solution

I have written some code (even Java) to accomplish what you require For my part, I need to display the scale factor as a percentage and a ratio The most common example is the zoom dialog box you see in the image editor, such as GIMP

You can find my code here in the updategratio () method, starting at line 1161 You can use the LGPL license as long as it suits you What I do basically follows what I do in gimp - this is one of the only effective and wise methods

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