问题描述
是什么ç原始数字之间的差异?我知道他们是什么,以及如何使用它们(有点),但我不知道每个人的能力和用途是什么。任何人都可以澄清哪些是最适合某些情况下,而不是别人?
What is the difference between C primitive numbers? I know what they are and how to use them (somewhat), but I'm not sure what the capabilities and uses of each one is. Could anyone clear up which ones are best for some scenarios and not others?
- 问题
- int
- float
- double
- long
- short
我可以存储与每个人吗strong>我知道有些可以存储更多的precise号码有的只能存储整数。例如说我想存储纬度(可能是从 CLLocation
对象中检索),哪一个我应该使用以避免丢失任何数据?
What can I store with each one? I know that some can store more precise numbers and some can only store whole numbers. Say for example I wanted to store a latitude (possibly retrieved from a CLLocation
object), which one should I use to avoid loosing any data?
我也注意到,有每一个无符号
的变种。这是什么意思,以及如何从一个原始的数量不是无符号的不同?
I also noticed that there are unsigned
variants of each one. What does that mean and how is it different from a primitive number that is not unsigned?
苹果有一些这方面的有趣的文档,但它并不能完全满足我的问题。
Apple has some interesting documentation on this, however it doesn't fully satisfy my question.
推荐答案
嗯,首先类型,如 INT
,浮动
,双击
,长
和短
是 C 的原语,而不是Objective-C的。正如你可能知道,Objective-C的排序是C的超集的Objective-C的的NSNumber
是所有这些类型的包装类。
Well, first off types like int
, float
, double
, long
, and short
are C primitives, not Objective-C. As you may be aware, Objective-C is sort of a superset of C. The Objective-C NSNumber
is a wrapper class for all of these types.
所以我会回答关于使用这些C原语你的问题,以及如何的Objective-C间$ P $其中pts他们。基本上,每个数字类型可以放在两类:整数类型和浮点类型即可。
So I'll answer your question with respect to these C primitives, and how Objective-C interprets them. Basically, each numeric type can be placed in one of two categories: Integer Types and Floating-Point Types.
- 短
- INT
- 长
- 长长
这些只能存储,好了,整数(整数),和的特点是两个特点:尺寸的和的符号性的
These can only store, well, integers (whole numbers), and are characterized by two traits: size and signedness.
的尺寸的意思怎么在电脑多少物理内存类型需要存储,也就是多少字节。从技术上讲,分配给每个类型的确切的内存是实现dependendant,但也有一些保证:(1)的字符
将永远是1个字节的(2)的的sizeof(短)< = sizeof的(INT)< = sizeof的(长)< = sizeof的(很长很长)
。
Size means how much physical memory in the computer a type requires for storage, that is, how many bytes. Technically, the exact memory allocated for each type is implementation-dependendant, but there are a few guarantees: (1) char
will always be 1 byte (2) sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
.
的符号性的手段,简直类型是否可以重新present负值。因此,一个有符号整数,或 INT
,可以重新present一定范围内负或正数(传统2,147,483,648 2,147,483,647),而一个无符号整数,或 unsigned int类型
可以重新present同一范围内的数字,但所有正面(0〜4294967295)。
Signedness means, simply whether or not the type can represent negative values. So a signed integer, or int
, can represent a certain range of negative or positive numbers (traditionally –2,147,483,648 to 2,147,483,647), and an unsigned integer, or unsigned int
can represent the same range of numbers, but all positive (0 to 4,294,967,295).
- 浮动
- 双击
- 长双
这些是用来存储十进制值(又名分数),并且还通过尺寸分类。同样,你拥有的唯一真正保证就是的sizeof(浮动)LT; = sizeof的(双)&LT; = sizeof的(长双)
。浮点类型使用的是比较奇特的内存模型可能很难理解存储,而且我不会进入,但有一个优秀导游的。
These are used to store decimal values (aka fractions) and are also categorized by size. Again the only real guarantee you have is that sizeof(float) <= sizeof(double) <= sizeof (long double)
. Floating-point types are stored using a rather peculiar memory model that can be difficult to understand, and that I won't go into, but there is an excellent guide here.
有在的。介绍CPS教科书地段也有很好的资源。
There's a fantastic blog post about C primitives in an Objective-C context over at RyPress. Lots of intro CPS textbooks also have good resources.
这篇关于Objective-C的原始数字之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!