overflow lazarus / 62-General ---------------------------------------------------------------------- Seenkao · 2021-08-24 10:08 Приветствую! Меня интересует работа с числами: Single, Double и прочими. Мне нужно поймать переполнение при использовании. Но я не вижу ни каких способов реализации этого в паскале. Меня не интересует флаг в поле отладчика в Lazarus. Меня интересует возможность использования этого флага самому, как это происходит в ассемблере. Есть ли такая возможность в FreePascal/Lazarus? Google translate: Greetings! I am interested in working with numbers: Single, Double and others. I need to catch overflow while using. But I don't see any way to implement this in pascal. I am not interested in the flag in the debugger field in Lazarus. I am interested in the possibility of using this flag myself, as it happens in assembler. Is there such a possibility in FreePascal/Lazarus? ---------------------------------------------------------------------- Red_prig · 2021-08-24 10:27 В первых, маска исключений: SetExceptionMask(); модуль Math Во вторых, стандартный перехват исключений: try ... except механизм В третьих, ручная проверка: IsInfinite/IsNan модуль Math [Google translation: First, the exception mask: SetExceptionMask (); Math module Second, standard exception catching : try ... except mechanism Third, manual checking: IsInfinite / IsNan Math module ] [Edited to Add English translation - please do so in future OR post in the Russian sub-forum.] ---------------------------------------------------------------------- Seenkao · 2021-08-24 10:42 Благодарю! Иногда простые вещи долго ищутся... Google translate: Thanks to! Sometimes simple things are looking for a long time ... Quote from: Red_prig on August 24, 2021, 12:27:23 pm[Edited to Add English translation - please do so in future OR post in the Russian sub-forum.] Я задавал вопрос всем. )))) ---------------------------------------------------------------------- Seenkao · 2021-08-24 10:52 Больше нет ни каких решений помимо модуля Math? Google translate: No more solutions besides the Math module? ---------------------------------------------------------------------- Red_prig · 2021-08-24 12:06 assembler block inline ---------------------------------------------------------------------- rsz · 2021-08-24 12:55 Quote from: Red_prig on August 24, 2021, 02:06:50 pmassembler block inline  I don't know if this is possible without assembly, but here's an example for integers in x86_64: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;{$mode objfpc}{$H+} uses sysutils; function ArithmeticOverflow: Boolean; inline;begin  Result := False;  {$ASMMODE INTEL}  asm    jno @skip;    mov Result, True;    @skip:  end;end; procedure PrintOverflow;begin  if ArithmeticOverflow    then WriteLn('Overflow detected.')    else WriteLn('No overflow detected.');end; var  I: Int32;begin  I := High(I);  PrintOverflow;  I := I + 1;  PrintOverflow;end. For singles/doubles, there is precision loss and positive infinity that seems to be relevant as jno/jo doesn't work as it's not an overflow: https://stackoverflow.com/a/30727493 (it's for C# but should apply here too, as it's generally about overflows) https://wiki.freepascal.org/IEEE_754_formats Might want to make an overloaded function to handle singles/doubles separately. Premature optimizations and such are an exercise for the reader . ---------------------------------------------------------------------- Thaddy · 2021-08-24 15:17 https://www.freepascal.org/docs- html/current/prog/progsu64.html#x71-700001.2.64 So it in the docs. When used well it also catches internal assembler overflows. ---------------------------------------------------------------------- Seenkao · 2021-08-24 17:29 Quote from: Red_prig on August 24, 2021, 02:06:50 pmassembler block inline  похоже так и придётся делать. rsz, для Int32 код не сработает, надо Word.  google translate: for Int32 the code will not work, you need Word. Quote from: Thaddy on August 24, 2021, 05:17:57 pmhttps://www.freepascal.org/docs- html/current/prog/progsu64.html#x71-700001.2.64 So it in the docs. When used well it also catches internal assembler overflows. Как я могу использовать этот флаг в данном случае? Обрабатывать исключение? Но я знаю, что мне проще просто запросить состояние флага (в ассемблере). Но, для тестов наверно проверю, может конечный ассемблерный код подскажет как это должно выполнятся.  Google translate: How can I use this flag in this case? Handle the exception? But I know it's easier for me to just query the state of the flag (in assembler). But, for tests, I will probably check, maybe the final assembly code will tell you how it should be done. Всем спасибо! Буду как обычно через тестирование делать. Google translate: Thanks to all! I will do it as usual through testing.