floating-point-precision

Why two float variables with PHP_INT_MAX values are same unless one of them is added with value greater than 1025

我们两清 提交于 2021-02-08 15:53:59
问题 <?php $x=PHP_INT_MAX; echo ((float)($x+1026)==(float)($x))?'EQUAL':'Not Equal'; I know floating point arithmetic is not exact and $x and $x+1 are so close together that they are rounded to the same floating point value and it shows the output as EQUAL if you use any number between 1 and 1025 but its only after you use value beyond 1025 it will start giving output as 'Not Equal'. I want to know why? What's the reason behind it? Why only after 1025? 回答1: With float, your assumption $x == $x + 1

How to convert floating value to integer with exact precision like 123.3443 to 1233443?

╄→гoц情女王★ 提交于 2021-02-07 11:56:18
问题 Sample code: int main() { float f = 123.542; int i = (int)f; printf("%d\n",i); } 回答1: 123.3443 can't be exactly represented by a floating-point number -- in a 32-bit float, it's effectively represented as 16166984 / 131072 , which is actually 123.34429931640625, not 123.3443. (It's off by around 6.8 x 10^-7.) If this is really the result you want (which it's probably not), take a look at how IEEE-754 floats work, and pull out your favorite arbitrary-precision math suite. Once you understand

What precision are floating-point arithmetic operations done in?

倾然丶 夕夏残阳落幕 提交于 2021-02-07 06:29:06
问题 Consider two very simple multiplications below: double result1; long double result2; float var1=3.1; float var2=6.789; double var3=87.45; double var4=234.987; result1=var1*var2; result2=var3*var4; Are multiplications by default done in a higher precision than the operands? I mean in case of first multiplication is it done in double precision and in case of second one in x86 architecture is it done in 80-bit extended-precision or we should cast operands in expressions to the higher precision

Exhausting floating point precision in a (seemingly) infinite loop

本小妞迷上赌 提交于 2021-01-27 06:40:09
问题 I've got the following Python script: x = 300000000.0 while (x < x + x): x = x + x print "exec: " + str(x) print "terminated" + str(x) This seemingly infinite loop, terminates pretty quickly if x is a floating point number. But if i change x to 300000000 instead, it gets into an infinite loop (runs longer than a minute in my test). I think this is to do with the fact that it's exhausting the precision of a floating point number that can be represented in memory. Can someone provide a more

Exhausting floating point precision in a (seemingly) infinite loop

こ雲淡風輕ζ 提交于 2021-01-27 06:39:40
问题 I've got the following Python script: x = 300000000.0 while (x < x + x): x = x + x print "exec: " + str(x) print "terminated" + str(x) This seemingly infinite loop, terminates pretty quickly if x is a floating point number. But if i change x to 300000000 instead, it gets into an infinite loop (runs longer than a minute in my test). I think this is to do with the fact that it's exhausting the precision of a floating point number that can be represented in memory. Can someone provide a more

Java create BufferedImage with float precision

六眼飞鱼酱① 提交于 2021-01-24 02:37:46
问题 I created a map editor in Java. The problem is, I have steps for every byte value, so the map isn't smooth. Is it possible to change the BufferedImage raster data to float data and draw in float precision on it? 回答1: To answer your question, yes, you can create a BufferedImage with float precision. It is however a little unclear if this will help you solve your problem. In any case, here's working example code for creating a BufferedImage with float precision: public class FloatImage { public

Why does MySQL round floats way more than expected?

冷暖自知 提交于 2021-01-20 19:21:04
问题 UPDATE some_table SET some_float_field=1919.987 WHERE id=123 SELECT * FROM some_table WHERE id=123 where some_float_field is a field defined as "float" (without any specific size values). The expected resulting value would be 1919.987; instead, it is rounded to 1919.99 Why? A 32bit (single precision) float has enough precision for storing that correctly! 回答1: When you run the query: SELECT * FROM some_table WHERE id = 123 You are relying on the user interface to format the floating point

Why does MySQL round floats way more than expected?

情到浓时终转凉″ 提交于 2021-01-20 19:16:36
问题 UPDATE some_table SET some_float_field=1919.987 WHERE id=123 SELECT * FROM some_table WHERE id=123 where some_float_field is a field defined as "float" (without any specific size values). The expected resulting value would be 1919.987; instead, it is rounded to 1919.99 Why? A 32bit (single precision) float has enough precision for storing that correctly! 回答1: When you run the query: SELECT * FROM some_table WHERE id = 123 You are relying on the user interface to format the floating point

Why does MySQL round floats way more than expected?

大兔子大兔子 提交于 2021-01-20 19:15:56
问题 UPDATE some_table SET some_float_field=1919.987 WHERE id=123 SELECT * FROM some_table WHERE id=123 where some_float_field is a field defined as "float" (without any specific size values). The expected resulting value would be 1919.987; instead, it is rounded to 1919.99 Why? A 32bit (single precision) float has enough precision for storing that correctly! 回答1: When you run the query: SELECT * FROM some_table WHERE id = 123 You are relying on the user interface to format the floating point

Full precision display of floating point numbers in C++?

萝らか妹 提交于 2021-01-20 04:43:12
问题 I have read several topics about the display of floating point numbers display in C++ and I couldn't find a satisfying answer. My question is: how to display all the significant digits of a floating point numbers in C++ in a scientific format (mantissa/exponent) ? The problem is that all numbers do not have the same number of significant digits in base 10. For example a double has 15 to 17 significant decimal digits precision, but std::numeric_limits<double>::digits10 returns 15 and