找到你要的答案

Q:Floating Point Number Overflow [duplicate]

Q:浮点数溢出[复制]

This question already has an answer here:

I have to create a function where I need to square values. I am told that even if the initial value is not too big or is not too small, the value squared might still overflow (returns inf) or underflow (returns 0), and I need to figure out how to prevent that.

My problem is I do not even understand what numbers can cause an overflow when squared when the number itself is not too large.

I thought it might be for the not too small numbers, e.g. 1/3 with repeating decimal numbers, but MATLAB just turns that into 0.3333.

Can someone give me an example of such a number or explain what kind of numbers would cause this?

这个问题在这里已经有了答案:

我必须创建一个函数,我需要平方值。告诉我,即使初始值不能太大也不能太小,平方值仍可能溢出或下溢(返回INF)(回报率0),我需要找到如何防止。

我的问题是,当数字本身不太大时,我甚至不理解什么数字会导致溢出。

我想这可能是不小的数字,如1 / 3与重复的十进制数,而MATLAB就转到0.3333。

有人能给我举一个例子说明这个数字吗?

answer1: 回答1:

For underflow, let's consider Planck's constant: 6.626070040e-34

sqrt(6.626070040e-34)
ans =
   2.5741e-17

Well, that's apparently not small enough, let's go smaller:

sqrt(6.626070040e-340)
ans =
     0

There's your underflow.

Overflow can be seen the same way, just use big numbers:

sqrt(6.626070040e34)
ans =
   2.5741e+17

sqrt(6.626070040e340)
ans =
   Inf

Underflow means the numbers are too small for MATLAB to handle, overflow means they are too big for MATLAB to handle.

Thanks to @obchardon here are the numbers on my MATLAB R2012a 64bits system:

realmax('double') %//largest allowed double
ans =
  1.7977e+308
realmin('double') %//smallest allowed double
ans =
  2.2251e-308

Now that we know what the largest possible value is that MATLAB can handle, let's try going below that and square it:

(realmax('double')-10).^2
ans =
   Inf

so the number we tried to square here (realmax('double')-10) is allowable by MATLAB, but not squarable.

潜流,让我们考虑普朗克常数:6.626070040e-34

sqrt(6.626070040e-34)
ans =
   2.5741e-17

嗯,这显然不够小,让我们变小:

sqrt(6.626070040e-340)
ans =
     0

这是你的不足。

溢出可以看到同样的方式,只是使用大号码:

sqrt(6.626070040e34)
ans =
   2.5741e+17

sqrt(6.626070040e340)
ans =
   Inf

底流的编号为MATLAB处理太小,溢出意味着他们是MATLAB处理太大。

感谢@ obchardon这里是我r2012a 64bits MATLAB系统数:

realmax('double') %//largest allowed double
ans =
  1.7977e+308
realmin('double') %//smallest allowed double
ans =
  2.2251e-308

现在我们知道可能的最大价值是MATLAB可以处理,让我们试试下面和广场:

(realmax('double')-10).^2
ans =
   Inf

所以我们试图广场(最大正浮点数(‘’)10)允许通过MATLAB,但不可平方。

matlab  floating-point