php - I have a question about type conversion. When converting float type to int, I thought there might be a problem, but the result was not?
过去多啦不再A梦
过去多啦不再A梦 2017-06-12 09:19:20
0
6
741

For example, if I convert the float type 2.0000000000 to int, the underlying storage should be 1.999999999999999. There is a precision limit, so when I convert this number to int, I think it will become 1 (the decimal part is discarded)
But after testing several languages, they are still correct (php, java, go, js)
php:

js:

go:

java:

过去多啦不再A梦
过去多啦不再A梦

reply all(6)
滿天的星座

2.0 can be accurately expressed by floating point numbers. There should be no problems with this number

我想大声告诉你

Decimals are stored in scientific notation, not 1.999999..., they are processed during the forced transfer, not directly converted to a type

漂亮男人

Generally speaking, converting low precision to high precision, such as int->float, when converting high precision to low precision, you will lose precision, and the data results may become very surprising.

阿神

A few classic examples

(int)(0.57 * 100)

(int)((0.1+0.7)*10)

世界只因有你

It has no practical significance. If it really works like you think, the person who designed this language will definitely take it into consideration

Detailed understanding requires certain assembly principles, so don’t get too carried away

巴扎黑

Can I ask where you downloaded this programming panel? It seems good

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!