A precision error problem for LUA and C + +

Source: Internet
Author: User
Tags lua

Recently and the server colleague to deal with a game data problem, found that the server Lua script floating point data in the server C + + code is called the error may occur. Similarly, if the client uses Lua's built-in library of functions to manipulate floating-point numbers, such rounding errors are reproduced, with a simple example:

Local k = 1+122/100;
Local T = 250*k
Print ("T:", T)
Print ("Math t:", Math.floor (t))
Print (String.Format ("string t:%d", T))

Ideally T, Math T, and string T are all 555.

The actual output only T is 555,math T and string T are both 554.

So the client and server in the same set of calculation formula to calculate a game data, because the client is a pure script operation to get results, the server is the script operation of the intermediate result T in C + + code to continue processing, the middle of the accuracy error problem, The result of the client and server always has a slight numerical bias.

Specific accuracy error reasons if the reader knows, welcome to popular science bloggers.

A precision error problem for LUA and C + +

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.