C语言中的浮点算法 [英] Floating Point Algorithms in C

查看:121
本文介绍了C语言中的浮点算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在思考浮点数学在计算机上的工作方式,这让我很难理解公式背后的所有技术细节.我需要了解加法,减法,乘法,除法和余数的基础.有了这些,我就能制作出触发函数和公式.

I am thinking recently on how floating point math works on computers and is hard for me understand all the tecnicals details behind the formulas. I would need to understand the basics of addition, subtraction, multiplication, division and remainder. With these I will be able to make trig functions and formulas.

我可以猜出一些东西,但是还不清楚.我知道可以通过用信号标志,基数和尾数分隔4个字节的整数来确定一个固定点.有了这个我们有一个1位的标志,一个5位的基数和一个10位的尾数. 32位字对于浮点值非常合适:)

I can guess something about it, but its a bit unclear. I know that a fixed point can be made by separating a 4 byte integer by a signal flag, a radix and a mantissa. With this we have a 1 bit flag, a 5 bits radix and a 10 bit mantissa. A word of 32 bits is perfect for a floating point value :)

要在两个浮点数之间进行加法运算,我可以简单地尝试将两个尾数相加并将进位加到5位基数上?这是进行浮点数学运算(或者说是定点数学运算)的一种方法,还是我完全错了?

To make an addition between two floats, I can simply try to add the two mantissas and add the carry to the 5 bits radix? This is a way to do floating point math (or fixed point math, to be true) or I am completely wrong?

我看到的所有解释都使用公式,乘法等,并且对于我猜想的事情而言,它们看起来如此复杂,会更简单一些.我需要的解释更多地是针对初学者的,而不是数学家的.

All the explanations I saw use formulas, multiplications, etc. and they look so complex for a thing I guess, would be a bit more simple. I would need an explanation more directed to beginning programmers and less to mathematicians.

推荐答案

基数取决于表示形式,如果使用基数r = 2则永远无法更改,该数字甚至没有任何数据可告诉您哪个基数.我认为您错了,您的意思是指数.

The radix depends of the representation, if you use radix r=2 you can never change it, the number doesn't even have any data that tell you which radix have. I think you're wrong and you mean exponent.

要在浮点中添加两个数字,您必须通过旋转尾数使指数等于另一个.右边的一位表示指数+1,而左边的一位表示指数-1,当您拥有相同指数的数字时,可以将它们相加.

To add two numbers in floating point you must make the exponent one equal to another by rotating the mantissa. One bit right means exponent+1, and one bit left means exponent -1, when you have the numbers with the same exponent then you can add them.

Value(x)=尾数*基数^指数

Value(x) = mantissa * radix ^ exponent

adding these two numbers

    101011 * 2 ^ 13
    001011 * 2 ^ 12

would be the same as adding:

    101011 * 2 ^ 13
    000101 * 2 ^ 13

使指数彼此相等后,即可进行操作. 您还必须知道表示形式是否具有隐式位,我的意思是,最高有效位必须为1,因此通常,例如在iee标准中,它的已知位在那里,但是尽管用于操作,但它并未被表示.

After making exponent equal one to another you can operate. You also have to know if the representation has implicit bit, I mean, the most significant bit must be a 1, so usually, as in the iee standard its known to be there, but it isn't representated, although its used to operate.

我知道这可能有点令人困惑,而且我不是最好的老师,所以如果您有任何疑问,请问.

I know this can be a bit confusing and I'm not the best teacher so any doubt you have, just ask.

这篇关于C语言中的浮点算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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