| In my experience there are few things slower that float to string and string to float. And it seems so unnecessary. I always implemented round to a specific digit based on the built-in roundss/roundsd functions which are native x86-64 assembler instructions (i.e. https://www.felixcloutier.com/x86/roundsd). I do not understand why this would not be preferable to the string method. float round( float x, int digits, int base) {
float factor = pow( base, digits );
return roundss( x * factor ) / factor;
} I guess this has the effect of not working for numbers near the edge of it's range. One could check this and fall back to the string method. Or alternatively use higher precision doubles internally: float round( float x, int digits, int base ) {
double factor = pow( base, digits );
return (float)( roundsd( x * factor ) / factor );
} But then what do you do if you have a double rounded and want to maintain all precision? I think there is likely some way to do that by somehow unpacking the double into a manual mantissa and exponent each of which are doubles and doing this manually - or maybe using some type of float128 library (https://www.boost.org/doc/libs/1_63_0/libs/multiprecision/do...)... But changing this implementation now could cause slight differences and if someone was rounding then hashing this type of changes could be horrible if not behind some type of opt-in. |
I don’t know of truly fast algorithms for string to float, although I improved upon our CRT’s performance by 40%.