search

Home  >  Q&A  >  body text

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?

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梦2752 days ago818

reply all(6)I'll reply

  • 滿天的星座

    滿天的星座2017-06-12 09:21:20

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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:21:20

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

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-12 09:21:20

    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.

    reply
    0
  • 阿神

    阿神2017-06-12 09:21:20

    A few classic examples

    (int)(0.57 * 100)

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

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-12 09:21:20

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-12 09:21:20

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

    reply
    0
  • Cancelreply