如何在MATLAB/Octave中获得实数整数溢出? [英] How do I get real integer overflows in MATLAB/Octave?

查看:130
本文介绍了如何在MATLAB/Octave中获得实数整数溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为MATLAB/Octave中的某些VHDL代码开发一种验证工具.因此,我需要产生实际"溢出的数据类型:

I'm working on a verification-tool for some VHDL-Code in MATLAB/Octave. Therefore I need data types which generate "real" overflows:

intmax('int32') + 1
ans = -2147483648

稍后,如果我可以定义变量的位宽,这将很有帮助,但是现在这并不那么重要.

Later on, it would be helpful if I can define the bit width of a variable, but that is not so important right now.

当我建立一个类似C的示例时,变量会不断增加直到小于零为止,它会永远旋转:

When I build a C-like example, where a variable gets increased until it's smaller than zero, it spins forever and ever:

test = int32(2^30);
while (test > 0)
    test = test + int32(1);
end

我尝试的另一种方法是自定义的溢出"例程,每次更改数字后都会调用该例程.这种方法痛苦缓慢,不切实际,并且在所有情况下都无法正常工作.有什么建议吗?

Another approach I tried was a custom "overflow"-routine which was called every time after a number is changed. This approach was painfully slow, not practicable and not working in all cases at all. Any suggestions?

推荐答案

在MATLAB中,一种选择是重载处理

In MATLAB, one option you have is to overload the methods that handle arithmetic operations for integer data types, creating your own custom overflow behavior that will result in a "wrap-around" of the integer value. As stated in the documentation:

您可以定义或重载自己的 int*的方法(对于任何方法都可以 对象)放置适当的 内@int*文件夹中的命名方法 路径上的文件夹.键入help datatypes作为方法的名称 您可以超载.

You can define or overload your own methods for int* (as you can for any object) by placing the appropriately named method in an @int* folder within a folder on your path. Type help datatypes for the names of the methods you can overload.

文档的本页列出了算术运算符的等效方法.二进制加法运算A+B实际上是由函数plus(A,B)处理的.因此,您可以创建一个名为@int32的文件夹(放置在

This page of the documentation lists the equivalent methods for the arithmetic operators. The binary addition operation A+B is actually handled by the function plus(A,B). Therefore, you can create a folder called @int32 (placed in another folder on your MATLAB path) and put a function plus.m in there that will be used instead of the built-in method for int32 data types.

以下是如何设计重载的plus函数以创建所需的上溢/下溢行为的示例:

Here's an example of how you could design your overloaded plus function in order to create the overflow/underflow behavior you want:

function C = plus(A,B)
%# NOTE: This code sample is designed to work for scalar values of
%#       the inputs. If one or more of the inputs is non-scalar,
%#       the code below will need to be vectorized to accommodate,
%#       and error checking of the input sizes will be needed.

  if (A > 0) && (B > (intmax-A))  %# An overflow condition

    C = builtin('plus',intmin,...
                B-(intmax-A)-1);  %# Wraps around to negative

  elseif (A < 0) && (B < (intmin-A))  %# An underflow condition

    C = builtin('plus',intmax,...
                B-(intmin-A-1));  %# Wraps around to positive

  else

    C = builtin('plus',A,B);  %# No problems; call the built-in plus.m

  end

end

请注意,我调用了内置的plus方法(使用 BUILTIN 函数)执行我所知道的int32值的加法运算,不会遇到上溢/下溢问题.如果我改为使用操作A+B执行整数加法,则将导致对重载的plus方法的递归调用,这可能导致额外的计算开销,或者(在最坏的情况下,最后一行是C = A+B;)无限递归.

Notice that I call the built-in plus method (using the BUILTIN function) to perform addition of int32 values that I know will not suffer overflow/underflow problems. If I were to instead perform the integer addition using the operation A+B it would result in a recursive call to my overloaded plus method, which could lead to additional computational overhead or (in the worst-case scenario where the last line was C = A+B;) infinite recursion.

这是一个测试,显示了实际操作中的环绕式溢出行为:

Here's a test, showing the wrap-around overflow behavior in action:

>> A = int32(2147483642);  %# A value close to INTMAX
>> for i = 1:10, A = A+1; disp(A); end
  2147483643

  2147483644

  2147483645

  2147483646

  2147483647   %# INTMAX

 -2147483648   %# INTMIN

 -2147483647

 -2147483646

 -2147483645

 -2147483644

这篇关于如何在MATLAB/Octave中获得实数整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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