奇怪的无符号long long int行为

时间:2016-03-13 10:09:31

标签: c++ unsigned-long-long-int

我使用unsigned long long int进行一些计算,但是

std::cout << std::setprecision(30) << 900000000000001i64+4*pow(10, 16);

提供输出:40900000000000000

和这个

std::cout << std::setprecision(30) << 900000000000011i64+4*pow(10, 16);

提供输出:40900000000000008

现在我不知道发生了什么我试图删除i64尝试打印4*pow(10, 16)给出正确的结果40000000000000000也尝试直接打印40900000000000011,它打印出正确的结果。它适用于10 ^ 14的功率但在此之后它开始表现得很奇怪。

有人可以解释发生了什么吗?

2 个答案:

答案 0 :(得分:7)

你得到这个尴尬结果的原因是因为丢失了<!DOCTYPE html> <html> <head> <title>Search Student</title> <script type="text/javascript"> function searchBy() { var node = document.getElementById("search").value; var elem; var other_1, other_2; if(node=="roomnumber"){ elem = document.getElementById("roomnohide"); other_1 = document.getElementById("rollnohide"); other_2 = document.getElementById("namehide"); }else if(node == 'rollno'){ elem = document.getElementById("rollnohide"); other_1 = document.getElementById("roomnohide"); other_2 = document.getElementById("namehide"); }else if(node == 'name'){ elem = document.getElementById("namehide"); other_1 = document.getElementById("rollnohide"); other_2 = document.getElementById("roomnohide"); } try{ elem.style.visibility = "visible"; other_1.style.visibility = "hidden"; other_2.style.visibility = "hidden"; alert("Success"); }catch(e){ } } </script> </head> <body> <h1 align="center">Search Student</h1> <formset> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <table align="center"> <tr> <td>Search By : </td> <td> <select id="search" required onchange="searchBy()" > <option value="rollno">Roll No</option> <option value="name">Name </option> <option value="roomnumber">Room No</option> </select> </td> </tr> <tr id="roomnohide" style="visibility: hidden;"> <td> Room No</td> <td><input type="text" pattern="[0-9]{3}" maxlength=3 name="roomno"></input></td> </td> </tr> <tr id="namehide" style="visibility: hidden;"> <td> Name</td> <td><input type="text" name="name"></input></td> </td> </tr> <tr id="rollnohide" style="visibility: hidden;"> <td> Roll No</td> <td><input type="text" pattern="[0-9]{3}" name="rollno"></input></td> </td> </tr> </table> </form> </formset> </body> </html> 类型中最低有效位的值。 double尾数只能包含大约15个十进制数字和52个二进制数字(wiki)。

double将被转换为双重修整所有低位。在你的情况下,有3个。

示例:

900000000000001i64+4*pow(10, 16)

将产生结果:

std::cout << std::setprecision(30);
std::cout << 900000000000001i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000002i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000003i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000004i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000005i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000006i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000007i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000008i64 + 4 * pow(10, 16) << endl;
std::cout << 900000000000009i64 + 4 * pow(10, 16) << endl;

注意值舍入到2 3

答案 1 :(得分:0)

请尝试此代码(请注意那里的显式类型转换)。

typedef unsigned long long ULONG64; //Not a must, but just for code clarity

std::cout << std::setprecision(30) << 900000000000001i64 + (ULONG64)(4 * pow(10, 16));
//output -> 40900000000000001
std::cout << std::setprecision(30) << 900000000000011i64 + (ULONG64)(4 * pow(10, 16));
//output -> 40900000000000011

你必须告诉编译器对pow(...)函数返回的结果进行显式类型转换(到ULONG64),该函数是double类型。