Performance and inaccuracies of function fixedToFloat() #2381
Replies: 2 comments 3 replies
-
It makes sense except for the hard-coded number of digits. For example, your function doesn't do the right thing for 2.14 fixed numbers. |
Beta Was this translation helpful? Give feedback.
-
fixedToFloat (like floatToFixed) doesn't do any rounding, and is not slow Those are used when decompiling/compiling from/to binary font. We decided to not round any more when converting fixedToFloat on decompile (or when parsing from XML), see #737 for more info. I guess you meant |
Beta Was this translation helpful? Give feedback.
-
The docstring of function "fixedToFloat()" (in file 'Lib/fontTools/misc/fixedTools.py') warns about its poor performance, saying:
"This is pretty slow compared to a simple division. Use sporadically."
In addition, it will not always return an accurate result. For instance, when its value argument is set to 66191 (representing version 1.01), it will return 1.00999 instead; for an argument value of 137624 (i.e., version 2.10), it will return 2.09998; and there are more examples. (I actually found these example just by looking at the mscorefonts.)
I coded a version of the function that should solve both of these problems, and is much shorter than the original. It will return the correct floating-point value for any 16:16-bit fixed-point value with up to four decimal digits in its fractional part (more than four decimal digits won't fit in the 16 bits that are reserved for it anyway).
It does (what should be) the equivalent of a round(value/scale*10000), but in integer, instead of floating-point, arithmetic; then, it divides that result by 10000 (doing a floating-point division), to effectively move the minor version into the fractional part.
This is what I came up with:
Does this make sense?
Beta Was this translation helpful? Give feedback.
All reactions