C++:存储 13 位数字总是失败 [英] C++ : storing a 13 digit number always fails

查看:53
本文介绍了C++:存储 13 位数字总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C++ 编程,我必须在我的一个练习中存储大数字.我必须存储的最大数字是:9 780 321 563 842.

I'm programming in C++ and I have to store big numbers in one of my exercices. The biggest number i have to store is : 9 780 321 563 842.

每次我尝试打印数字(包含在变量中)时,它都会给我一个错误的结果(不是那个数字).

Each time i try to print the number (contained in a variable) it gives me a wrong result (not that number).

32 位类型是不够的,因为 2^32 是一个 10 位数字,我必须存储一个 13 位数字.但是使用 64 位,您可以表示具有 20 位数字的数字.所以我尝试使用uint64_t"类型,但这对我不起作用,我真的不明白为什么.

A 32bit type isn't enough since 2^32 is a 10 digit number and I have to store a 13 digit number. But with 64 bits you can respresent a number that has 20digits. So I tried using the type "uint64_t" but that didn't work for me and I really don't understand why.

所以我在互联网上搜索以找到哪种类型足以让我的变量适应.我在这个论坛上看到有同样问题的人,但他们使用 long long int 或 long double 作为类型解决了这个问题.但没有一个对我有用(long float 也没有).

So I searched on the internet to find which type would be sufficient for my variable to fit in. I saw on this forum persons with the same problem but they solved it using long long int or long double as type. But none worked for me (neither did long float).

我真的不知道还有哪种类型可以存储该号码,因为我尝试了很多但没有任何效果.

I really don't know which other type could store that number, as I tried a lot but nothing worked for me.

感谢您的帮助!:)

--代码有点长和复杂,对问题无关紧要,所以这实际上是我对包含该数字的变量所做的:

-- EDIT : The code is a bit long and complex and would not matter for the question, so this is actually what I do with the variable containing that number :

string barcode_s = "9780321563842";
uint64_t barcode = atoi(barcode_s.c_str()); 
cout << "Barcode is : " << barcode << endl;

当然,我不会将该数字放入变量(字符串类型)barcode_s"中以将其直接转换为数字,但这就是我的程序中发生的情况.我从输入文件中读取文本并将其放入barcode_s"(我读取并放入该变量的文本始终是一个数字),然后将该字符串转换为一个数字(使用 atoi).

Off course I don't put that number in a variable (of type string) "barcode_s" to convert it directly to a number, but that's what happen in my program. I read text from an input file and put it in "barcode_s" (the text I read and put in that variable is always a number) and then I convert that string to a number (using atoi).

所以我认为问题出在atoi"函数上?

So i presume the problem comes from the "atoi" function?

感谢您的帮助!

推荐答案

问题确实是 atoi:它返回一个 int,在大多数平台上是一个 32-位整数.从 int 转换为 uint64_t 不会神奇地恢复已经丢失的信息.

The problem is indeed atoi: it returns an int, which is on most platforms a 32-bits integer. Converting to uint64_t from int will not magically restore the information that has been lost.

不过,有几种解决方案.在 C++03 中,您可以使用 stringstream 来处理转换:

There are several solutions, though. In C++03, you could use stringstream to handle the conversion:

std::istringstream stream(barcode_s);
unsigned long barcode = 0;
if (not (stream >> barcode)) { std::abort(); }

在 C++11 中,您可以简单地使用 stoulstoull:

In C++11, you can simply use stoul or stoull:

unsigned long long const barcode = std::stoull(barcode_s);

这篇关于C++:存储 13 位数字总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆