如何“真实数字”存储在“浮动”中。用C编程时变量? [英] How a "REAL NUMBER" is stored in the "float" variable while programming in C ?

查看:65
本文介绍了如何“真实数字”存储在“浮动”中。用C编程时变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
main()
{
float a,b;
printf("Enter two real number values for a and b :\n");
scanf("%f %f",&a,&b);
printf("a = %f and b = %f\n",a,b);
getch();
}



输入:

456987.2

456987.3

输出:

456987.187500

456987.312500



为什么我的输出异常?我的意思是为什么我只获得近似值作为输出?


Input :
456987.2
456987.3
Output :
456987.187500
456987.312500

Why is my output abnormal ? I mean why am i getting only approximate values as output ?




我正在使用Dev-C ++ 5.11 IDE。

我的电脑有64位建筑。



我尝试过:



我试过猜测要存储的实数值的近似背后的原因,但我无法得出解释。


I am using Dev-C++ 5.11 IDE.
My computer has 64-bit architecture.

What I have tried:

I have tried to guess the reason behind the approximation of the real number values to be stored, but i can't come up with an explanation.

推荐答案

这是在整个地方记录的,成千上万次。你要找的是 IEEE浮点数表示 [ ^ ]。
This is documented all over the place, thousands of times. What you're looking for is "IEEE floating point number representation[^]".


当您将某个变量声明为 float double时,你告诉计算机它是浮点数据,是数学实数的近似表示。你自己称之为真实,但是你需要理解实数的数学概念。请看我对这个问题的评论。



它们是按定义的近似值;它们原则上不准确。



编译不能知道你可以分配给浮点值的内容。在下一步,您可以计算acos(-1)。尽管-1可以被认为是精确值,但结果是超越数字π。你有没有听说过可以证明任何给定的有限数字序列可以在这个数字的无限表示中找到?



也许你需要分数但精确的数字?它们并不难实现。它可以是两个整数的记录(结构):尾数和指数(例如,10的幂)。粗略地说,您定义一个整数,并单独定义小数点放置位置的信息。然后您可以定义这些数字的所有操作。当然,它们并不像真实。你甚至不能除以1/3。或者你可以,如果你使它更高级:添加期间的信息,你可以计算和表示:1/3 = 0.(3)。但是如果你迈出这一步,你可以更好地实现有理数的概念,它是一对整数: numerator 分母。 />


使用浮点数,您可能还想学习数字数据格式。



-SA
When you declare some variable as float or double, you tell the computer that it's floating-point data, approximate representation of mathematical real numbers. You call them "real" yourself, but then you need to understand the mathematical concept of real numbers. Please see my comment to the question.

They are approximate by definition; and they cannot be precise in principle.

The compile cannot "know" what you may assign to a floating-point value. On next step, you can calculate acos(−1). Even though −1 can be considered as exact value, the result is transcendental number π. Did you ever heard that it can be proven that any given finite sequence of digits can be found in the infinite representation of this number?

Maybe you need fractional but precise number? They are not hard to implement. It could be a record (struct) of two integer numbers: a mantissa and an exponent (say, a power of 10). Roughly, you define an integer number and, separately, the information on where to put a decimal point. And then you can define all operations on these number. Of course, they are nothing like real. You cannot even divide 1 / 3. Or you can, if you make it more advanced: add information on the period, which you can calculate and represent: 1/3 = 0.(3). But if you make this step, you can better implement the concept of rational numbers which is a pair of integers: numerator and denominator.

With floating-points, you also may want to learn numeric data formatting.

—SA


456987.187500是可以编码为456987.2的单精度浮点数的最接近的数字。更改最低有效位会给出一个与456987.2有较大差异的值。



大多数系统上的浮点数(从内存中)有24位有效位,大约是7或8位有效小数。一旦你想尝试用更多的有效位代表一个数字,它就会出现可怕的错误。



如果你需要更多的精确度尝试使用双倍或长双。如果这还不够,您可能需要查看滚动自己的浮点表示或借用别人的。
456987.187500 is the closest number that can be encoded as a single precision floating point number to 456987.2. Changing the least significant bit gives you a value that has a greater difference to 456987.2.

A float on most systems has (from memory) 24 significant bits, which is about 7 or 8 significant decimal digits. As soon as you want to try and represent a number with a greater number of significant bits it'll all go horribly wrong.

If you need more bits of precision try using a double or long double. If that's not enough you might need to look at rolling your own floating point representation or borrowing someone else's.


这篇关于如何“真实数字”存储在“浮动”中。用C编程时变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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