Objective-C数量类型-基本数据类型的范例代码

来源:互联网 发布:宿迁湖滨新区网络问政 编辑:程序博客网 时间:2024/05/11 16:28
以下内容是范例4-1,展示基本数据类型的输出:

01 #import <Foundation/Foundation.h>
02 
03 int main (int argc, const char * argv[])
04 {
05     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06 
07     int        integerVar = 100;
08     float    floatingVar = 331.79;
09     double    doubleVar = 8.44e+11;
10     char    charVar = 'W';
11     
12     NSLog(@"integerVar = %i", integerVar);
13     NSLog(@"floatingVar = %f", floatingVar);
14     NSLog(@"doubleVar = %e", doubleVar);
15     NSLog(@"doubleVar = %g", doubleVar);
16     NSLog(@"charVar = %c", charVar);
17     
18     [pool drain];
19     return 0;
20 }

最终输出结果:

integerVar = 100

floatingVar = 331.790009

doubleVar = 8.440000e+11

doubleVar = 8.44e+11

charVar = W


 

在输出的第二行,大家会注意到指派给floatingVar的值331.79,实际显示成了331.790009。事实上,实际显示的值是由特定计算机系统决定的。出现这种不准确值的原因是:计算机内部使用特殊的方式表示数字。使用计算器处理数字时,很可能遇到相同的不准确性。如果用计算器计算1除以3,将得到结果.33333333,很可能结尾带有一些附加的3。这串3是计算器计算1/3的近似值。理论上,应该存在无限个3。然后该计算器只能保存这些位的数字,这就是计算机的不确定性。此处应用了相同类型的不确定性:在计算机内存中不能精确地表示一些浮点值

苹果开发者Mike在CSDN上传的源码 Data_Type
原创粉丝点击