Determination of equality of values of numerical Variables

Source: Internet
Author: User

It is customary to use = when determining whether two quantities are equal, but such use involves a great risk. Let's look at an example.

1 nominal = 1.0 2 sum = 0.0 3 4 for I = 1, 10 do 5 sum = sum + 0.1 6 end 7 8 print (nominal = sum, nominal, sum) 9 10 11 12 x = 113 y = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.114 + 1.015 z = 16 print (x = y, x = z, y = z, x, y, z)

Guess what the output is, which are true and false. Run

Furthermore, we can know that the result of x-y> 0 is true, which is rather confusing. It only accumulates 10 times, and 0.1 is not a very extreme value. It can only be sighed. If you are interested, try the results in C ++ and java.

 

A better way is to write the equality judgment function by yourself. The value variable can determine whether the difference value should be equal by itself. This conclusion should be kept in mind. Remember to use it later, or the bug will be very depressing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.