什么类型的数据适用于无限大的变量? [英] What type of data is good for infinitely large variables?

查看:340
本文介绍了什么类型的数据适用于无限大的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是这个网站的新手和编码。好吧,我并不是全新的,因为我已经尝试了一些Excel VBA和HTML和CSS。我只是C ++的新手。



我正在教自己如何使用 C ++ Programming for Absolute Beginner,2nd Edition 在C ++中编码。



虽然我已经完成了第1章的挑战之一,但我注意到该程序并不完美。



Hi everyone,

I am new to this site and to coding. Well, I am not completely new, as I have experimented with a bit of Excel VBA and HTML and CSS. I am just new to C++.

I am teaching myself how to code in C++ using C++ Programming for the Absolute Beginner, 2nd Edition.

Although I have done one of the Chapter 1 challenges, I notice that the program is imperfect.

/* Write a program that asks users for their names, that greets them, 
and that asks them for 2 numbers and then provides the sum. */

#include <iostream>
#include <string>

using namespace std;

int main(void)

{
	using std::cout;
	using std::cin;
	using std::string;
	string name = "";
	short firstNumber, secondNumber, Sum;
	//ask for name
	cout << "What is your name?\n";
	cin  >> name;
	//greet user
	cout << "\nHello " << name.c_str() << "\n";
	cout << "I am a smart computer. Give me 2 numbers, and I will add ";
	cout << "them together.\n";
	//ask 2 numbers
	cout << "Input two numbers to add. \n";
	cin  >> firstNumber >> secondNumber;
	Sum = firstNumber + secondNumber;
	cout << "Your sum is: " << Sum << "\n"; 
	return 0;
}





我的尝试:



作为刚读过变量章节的初学者,我知道我使用的是短数据类型,它只能存储有限数量的整数。 Long数据类型也只能存储有限数量的整数。是否有可能存储无限大数的数据类型或仅存储非常非常大的数字,如100,000,000,000,000,000,000,000,000,000或10 ^ 29或100 octillion?



What I have tried:

As a beginner who has just read the chapter on variables, I know that I used the short datatype, which can only store a limited amount of integers. Long datatype also can only store a limited amount of integers. Is there a possible datatype that can store an infinitely big number or just a very, very, very big number like 100,000,000,000,000,000,000,000,000,000 or 10^29 or one hundred octillion?

推荐答案

使用Google查看 BigIntegers BigNum 是否符合您的需求。

BigIntehers 可以集成到您的编译器库中,具体取决于它的年龄。





GNU MP Bignum图书馆 [ ^ ]
Use Google and see if BigIntegers or BigNum fit your needs.
BigIntehers can be integrated in your compiler libraries depending on how old it is.


The GNU MP Bignum Library[^]


我不知道什么是大变量,忘记无限大变量。任何计算机都是有限状态机。它能告诉你什么吗?它计算,有一个无限的概念,但它是一个抽象,更确切地说,是一些抽象。



另外,数据的大小不是与变量的大小相同。



现在,您应该看到两个相关概念之间的巨大差异。一件事是非常大的数字。可以使用浮点数来表示这些数字。要理解的第一件事是:真正大数字的表示可以携带相当少量的信息。这不是一个大数字:999 999 999 ? :-)但它带有非常适量的信息。另见:

浮点 - 维基百科,免费的百科全书 [ ^ ],

IEEE浮点 - 维基百科,免费的百科全书 [ ^ ]。



另一件事是持有大量信息的数字,理想情况下,只有可用计算机限制的任意大小记忆。为简单起见,让我们把诸如任意大小的实数的表示这样复杂的东西放在一边。让我们考虑任意整数,精确表示,没有任何近似值。显然,这些数字可以表示为任意大小的比特序列,仅受可用存储器的限制。此类型的通常名称是大整数,或者,对于类型名称, BigInt BigInteger 。一些这样的类型实际上是无限大小的,但在C ++中,更常见的是开发更弱的类型,其中最大尺寸在对象初始化时受到限制。



这类课程的开发甚至都不是很困难。基本上,你只是模仿小学算术中学到的操作。只有我不建议使用字符串来表示0..9位的序列;这样的表示完全没有效率。它应该是比特序列。您可以进行搜索并找到许多可用的实施方案(质量不同:-))。



-SA
I don't know what is "large variable", forget "infinitely large variable". Any computer is a finite-state machine. Does it tell you something? It computing, there is a concept of "infinity", but it's an abstraction, more exactly, a number of abstractions.

Also, the size of data is not the same as size of a variable.

Now, you should see the big difference between two related concepts. One thing is "very large number". Such numbers can be represented using floating-point number. First thing to understand is: representation of a really big number can carry pretty little amount of information. Isn't it a big number: 999999999? :-) But it carries very modest amount of information. See also:
Floating point — Wikipedia, the free encyclopedia[^],
IEEE floating point — Wikipedia, the free encyclopedia[^].

Another thing is the number holding big volume of information, ideally, of the arbitrary size limited only by available computer memory. For simplicity, let's set aside such complicated thing as representation of arbitrary-size real numbers. Let's consider arbitrary integer number, represented precisely, without any approximation. Apparently, such numbers can be represented as a sequence of bits of arbitrary size, limited only by available memory. The usual name for such types is "big integer", or, for a type name, BigInt, BigInteger. Some of such types are really of unlimited size, but in C++ it's more usual to develop much weaker type where the maximum size is limited at the moment of object initialization.

The development of such classes is not even extremely difficult. Basically, you just mimic the operations learned in elementary schools arithmetic. Only I would not advice using strings to represent the sequence of 0..9 digits; such representation is utterly inefficient. It should be sequence of bits. You can do your search and find a number of available implementations (of different quality :-)).

—SA


请参阅 C ++数据类型范围

[ ^ ]


这篇关于什么类型的数据适用于无限大的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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