How do I get real integer overflows in MATLAB/Octave?

后端 未结 6 1608
天涯浪人
天涯浪人 2020-11-28 14:31

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         


        
6条回答
  •  日久生厌
    2020-11-28 14:58

    I'm not a Java expert, but underlying Java classes available in Matlab should allow handling of overflows like C would. One solution I found, works only for single value, but it converts a number to the int16 (Short) or int32 (Integer) representation. You must do your math using Matlab double, then convert to Java int16 or int32, then convert back to Matlab double. Unfortunately Java doesn't appear to support unsigned types in this way, only signed.

    double(java.lang.Short(hex2dec('7FFF')))
    
    ans = 32767 double(java.lang.Short(hex2dec('7FFF')+1))
    ans = -32768 double(java.lang.Short(double(intmax('int16'))+1))
    ans = -32768 double(java.lang.Integer(hex2dec('7FFF')+1))
    ans = 32768

    https://www.tutorialspoint.com/java/lang/java_lang_integer.htm

提交回复
热议问题