| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2894 人关注过本帖
标题:[求助]刚学菜鸟求教一个输出的问题!
只看楼主 加入收藏
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 

当然,你输入的值超出了double所能精确表达的范围,结果当然是有误差的。

注意double的精度。


九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2004-08-19 10:50
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
本文获得 主题 float的存储结构(建议不清楚的人看看)
manesking manesking 发表于 2004-5-2 12:43:38 [200分]
我们知道: int是用首位代表符号,其余代表代表数值来表示一个有符号整数的; 而unsigned是来那个符号位也变成了数值的最高位,所以相应的大了一倍; 那么,float是如何表示的呢?即float的各位的含义是什么? 可能还有很多人还不知道吧,一些书上可能也没有.欢迎大家踊跃抢答! 以后有有趣的问题,我还会再考考大家的:) 正解: float(4bytes)的结构如我17楼的贴图; 其中指数部分的8位值还要减127才是实际的指数,即:2^(n-127); 而有效数字是两个指数级之间的一个精度,来确定实数值落在两个指数级之间的具体位置; 例: 0x3f800000 // 0 01111111 000000000000000 代表 1.0 // 指数127,代表2^0,所以就是1; // 有效数字为0,不代表0,而是代表正好是1~2之间最低的位置,所以是1.0; 0x3fc00000 // 0 01111111 100000000000000 代表 1.5 // 指数同样是0,是1~2之间的数; // 而有效数字正好是所能表达的一半那么大,所以就是1~2的中央,是1.5; 0x40000000 // 0 10000000 000000000000000 代表 2.0 // 指数为128-127=1,所以是2,后面都是0,代表正好是2; 0x7f800000 // 0 11111111 000000000000000 正无穷 // 因为指数为所能表示的最大,以上没有再高的指数,所以有效数字必须为0,否则如果有效数字非0,就是非法的! 0x00000001 // 0 00000000 000000000000001 最小的正实数 // 如果全为0,就是代表0,所以这是比0只大一点点的:2^(-127) 2004-5-9 10:35:12?manesking 对内容进行了修改

------------------------签----名---- /*热心斑竹*/ E_mail:manesking@163.com MSN:sunxiangpost@hotmail.com

manesking manesking 第1楼 回复于2004-5-2 12:48:43
或者谁可以写一个函数: int float_2_int(float f); 也算对,但不要告诉我是:return (int)(f); 不可以使用强制转换!
manesking manesking 第2楼 回复于2004-5-2 14:06:45
难道是太难了吗?
3017 3017 第3楼 回复于2004-5-2 14:48:25
是啊 好难 俺怎么也想不通 楼主能否给出答案 俺也学习一下 谢谢
fishyu Fish YU 第4楼 回复于2004-5-2 18:09:32
看看《计算机原理》吧
dataflow 静水流 第5楼 回复于2004-5-2 20:20:38
float类型精度由具体编译器实现确定,下面的代码可以看位的信息。大家可分析以下。 #include<iostream> #include <bitset> #include <string> using sdt::cout; using std::bitset; int main(int argc, _TCHAR* argv[]) { float a = 1.0e+1f; float b = -1.0e+2f; float c = 1.0e+3f; bitset<32> bits; memcpy(&bits,&a,4); cout<<bits<<"\n"; memcpy(&bits,&b,4); cout<<bits<<"\n"; memcpy(&bits,&c,4); cout<<bits<<"\n"; } 执行结果输出:(在vc++.net上) 01000001001000000000000000000000 11000010110010000000000000000000 01000100011110100000000000000000 2004-5-2 20:27:41?dataflow 对内容进行了修改 2004-5-3 4:19:58?dataflow 对内容进行了修改
manesking manesking 第6楼 回复于2004-5-3 7:29:29
楼上只是给出数据的2进制形式,并没有解释各位的含义. 或者,我给你一个实数,你能不用编程推算出各位的值吗? 或者,我给你一个2进值数,你能推算出它代表的实数值吗?
jassen jassen 第7楼 回复于2004-5-3 10:43:58
c/c++本来支持位运算,这些问题应该不难吧
dataflow 静水流 第8楼 回复于2004-5-3 16:52:05
msdn2003中记述: Microsoft Visual C++ 与 IEEE 数值标准一致。存在实数的三种内部变化。在 Visual C++ 中使用 Real*4 和 real*8。Real*4 用字 float 声明。Real*8 用字 double 声明。在 Windows 32 位编程中,long double 数据类型映射为 double。 IEEE 754标准文档中描述: Storage Layout IEEE floating point numbers have three basic components: the sign, the exponent, and the mantissa. The mantissa is composed of the fraction and an implicit leading digit (explained below). The exponent base (2) is implicit and need not be stored. The following figure shows the layout for single (32-bit) and double (64-bit) precision floating-point values. The number of bits for each field are shown (bit ranges are in square brackets): Sign Exponent Fraction Bias Single Precision 1[31] 8[30-23] 23[22-00] 127 Double Precision 1[63] 11[62-52] 52[51-00] 1023 The Sign Bit The sign bit is as simple as it gets. 0 denotes a positive number; 1 denotes a negative number. Flipping the value of this bit flips the sign of the number. The Exponent The exponent field needs to represent both positive and negative exponents. To do this, a bias is added to the actual exponent in order to get the stored exponent. For IEEE single-precision floats, this value is 127. Thus, an exponent of zero means that 127 is stored in the exponent field. A stored value of 200 indicates an exponent of (200-127), or 73. For reasons discussed later, exponents of -127 (all 0s) and +128 (all 1s) are reserved for special numbers. For double precision, the exponent field is 11 bits, and has a bias of 1023. The Mantissa The mantissa, also known as the significand, represents the precision bits of the number. It is composed of an implicit leading bit and the fraction bits. To find out the value of the implicit leading bit, consider that any number can be expressed in scientific notation in many different ways. For example, the number five can be represented as any of these: 5.00 x 100 0.05 x 102 5000 x 10-3 In order to maximize the quantity of representable numbers, floating-point numbers are typically stored in normalized form. This basically puts the radix point after the first non-zero digit. In normalized form, five is represented as 5.0 x 100. A nice little optimization is available to us in base two, since the only possible non-zero digit is 1. Thus, we can just assume a leading digit of 1, and don't need to represent it explicitly. As a result, the mantissa has effectively 24 bits of resolution, by way of 23 fraction bits.

[此贴子已经被作者于2004-08-19 10:58:42编辑过]


九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2004-08-19 10:56
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
double的一下子没有找到,找到贴上来给你。我的服务还可以吧?300不多吧?

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2004-08-19 11:09
天地一沙鸥
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2004-8-14
收藏
得分:0 
以下是引用live41在2004-08-18 23:50:47的发言:

这个方法乌鸦已经提了,不过怎么把“浮点数 => 字符串”

这里也麻烦啊!

乌鸦的方法是输入字符串,然后判断~~这点被你用 不符合大工程 顶回去了,所以我根本没必要再说. 还有 浮点数转成字符串只用一个函数就可以了,没有必要像他们一样写得这个一大堆.

函数: char *gcvt(double value, int ndec, char *buf) 各个参数的意义:value是你想要转换的浮点数; ndec 是转换的有效数字位数; *buf 是转换后的字符串. 举个例子:

main() { char str[25]; double num;

num=9.876; gcvt(num,4,str);

printf("String= %s\n",str);

getch(); }

看清楚了吗? 浮点转换成字符串之后我想你应该知道该做些什么了吧~~


鸟凄声以孤归, 兽索偶而不还。 悼当年之晚暮, 恨兹岁之欲殚。 思宵梦以从之, XXX而不安; 若凭舟之失棹, 譬缘崖而无攀。 /img/assets/200401/200401061015134010607.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://joke./img/assets/200401/200401061015134010607.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
2004-08-19 12:22
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
以下是引用knocker在2004-08-19 11:09:21的发言: double的一下子没有找到,找到贴上来给你。我的服务还可以吧?300不多吧?
还好,够信誉,下次再惠顾。
2004-08-19 12:42
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
以下是引用天地一沙鸥在2004-08-19 12:22:22的发言:

乌鸦的方法是输入字符串,然后判断~~这点被你用 不符合大工程 顶回去了,所以我根本没必要再说. 还有 浮点数转成字符串只用一个函数就可以了,没有必要像他们一样写得这个一大堆.

函数: char *gcvt(double value, int ndec, char *buf) 各个参数的意义:value是你想要转换的浮点数; ndec 是转换的有效数字位数; *buf 是转换后的字符串. 举个例子:

main() { char str[25]; double num;

num=9.876; gcvt(num,4,str);

printf("String= %s\n",str);

getch(); }

看清楚了吗? 浮点转换成字符串之后我想你应该知道该做些什么了吧~~

噢~~~竟然有这样一个函数,不早说~~~
2004-08-19 12:44
天使预备役
Rank: 2
等 级:论坛游民
威 望:3
帖 子:670
专家分:10
注 册:2004-4-6
收藏
得分:0 

#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> void main() { double num; long a1=0,a2=0; char str[50]; FILE *fp; clrscr(); if((fp=fopen("e:\\bc31\\num.txt","wb"))==NULL) { printf("ERROR"); exit(0); } scanf("%lf",&num);//先把数据以双精度浮点数存入文件! fprintf(fp,"%lf",num); fclose(fp); if((fp=fopen("e:\\bc31\\num.txt","rb"))==NULL) { printf("ERROR!"); exit(0); } rewind(fp); fscanf(fp,"%50s",str); sscanf(str,"%ld.%ld",&a1,&a2);//或去掉上一句直接写成fscanf(fp,"%ld.%ld",&a1,&a2);也可以! printf("%s*%ld.%ld\n",str,a1,a2); while(1) {//因为后面多出的数肯定都是零,所以余10不为0,就是小数部分。 if(a2%10==0) a2=a2/10; else break; } printf("The result is:%ld\n",a2); fclose(fp); getche(); }

[此贴子已经被作者于2004-08-19 16:53:46编辑过]


差点把你忘了...
2004-08-19 16:47
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
以下是引用天使预备役在2004-08-19 16:47:22的发言:

#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> void main() { double num; long a1=0,a2=0; char str[50]; FILE *fp; clrscr(); if((fp=fopen("e:\\bc31\\num.txt","wb"))==NULL) { printf("ERROR"); exit(0); } scanf("%lf",&num);//先把数据以双精度浮点数存入文件! fprintf(fp,"%lf",num); fclose(fp); if((fp=fopen("e:\\bc31\\num.txt","rb"))==NULL) { printf("ERROR!"); exit(0); } rewind(fp); fscanf(fp,"%50s",str); sscanf(str,"%ld.%ld",&a1,&a2);//或去掉上一句直接写成fscanf(fp,"%ld.%ld",&a1,&a2);也可以! printf("%s*%ld.%ld\n",str,a1,a2); while(1) {//因为后面多出的数肯定都是零,所以余10不为0,就是小数部分。 if(a2%10==0) a2=a2/10; else break; } printf("The result is:%ld\n",a2); fclose(fp); getche(); }

我晕~~~你的代码好像太那个了吧~~~杀鸡焉用恐龙刀!再说,效率不高~~~

2004-08-19 18:45
快速回复:[求助]刚学菜鸟求教一个输出的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012994 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved