Index: cpp/common/nonstd_math.cpp
===================================================================
--- cpp/common/nonstd_math.cpp	(revision 1215)
+++ cpp/common/nonstd_math.cpp	(revision 1251)
@@ -1,4 +1,4 @@
 // This file is a part of Framsticks SDK.  http://www.framsticks.com/
-// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
+// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
 // See LICENSE.txt for details.
 
@@ -30,6 +30,6 @@
 		(char*)
 #endif
-		str.data(); //now we will be operating directly on the internal std::string buffer
-		for (int i = str.length() - 1, end = str.length(); i >= 0; i--) //remove trailing zeros, and maybe also '.'
+			str.data(); //now we will be operating directly on the internal std::string buffer
+		for (int i = int(str.length()) - 1, end = int(str.length()); i >= 0; i--) //remove trailing zeros, and maybe also '.'
 		{
 			if (s[i] == '0')
@@ -76,5 +76,5 @@
 		strncpy(buffer, s.c_str(), std::min(bufferlen, (int)s.length() + 1));
 		buffer[bufferlen - 1] = 0; //ensure the string is truncated
-		return s.length();
+		return int(s.length());
 	}
 }
@@ -91,4 +91,7 @@
 
 
+// Idea: enable selected floating point exceptions when the app starts and disable them temporarily when dividing values in ExtValue, so that we can directly handle problematic cases there.
+// This allows to catch problematic situations when the program performs calculations using NaN, INF etc.
+
 #ifdef IPHONE
 //TODO! -> ? http://stackoverflow.com/questions/12762418/how-to-enable-sigfpe-signal-on-division-by-zero-in-ios-app
@@ -121,24 +124,27 @@
 #include <fenv.h>
 
-void fpExceptInit()
-{}
-
-void fpExceptEnable()
-{
-	feclearexcept(FE_DIVBYZERO);
-	feenableexcept(FE_DIVBYZERO);
-}
-
-void fpExceptDisable()
-{
-	fedisableexcept(FE_DIVBYZERO);
-}
-
-#endif
-
-
-
-#ifdef __BORLANDC__
-// there was once a problem like this:
+static constexpr int WANTED_FP_EXCEPTIONS = FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW;
+
+void fpExceptInit()
+{}
+
+void fpExceptEnable()
+{
+	feclearexcept(WANTED_FP_EXCEPTIONS);
+	feenableexcept(WANTED_FP_EXCEPTIONS);
+}
+
+void fpExceptDisable()
+{
+	fedisableexcept(WANTED_FP_EXCEPTIONS);
+}
+
+#endif
+
+
+
+#if defined(__BORLANDC__) || defined(_MSC_VER)
+
+// in Borland, there was once a problem like this:
 // http://qc.embarcadero.com/wc/qcmain.aspx?d=5128
 // http://www.delorie.com/djgpp/doc/libc/libc_112.html
@@ -150,44 +156,47 @@
 // But it was resolved by restarting windows and cleaning all intermediate compilation files :o (restarting windows was the key element! restarting BC++Builder and deleting files would not help)
 
-#include "log.h"
+
+#if defined(__BORLANDC__) // adding a missing constant and a function
+#define _MCW_EM         0x0008001f              // Interrupt Exception Masks - from Visual C++'s float.h
+
+void _controlfp_s(unsigned int* _CurrentState, unsigned int  _NewValue, unsigned int  _Mask) //pretends to be the real _controlfp_s() function
+{
+	*_CurrentState = _control87(_NewValue, _Mask);
+}
+#endif
+
+#if defined(_MSC_VER) 
+#pragma fenv_access (on)
+#endif
+
+// http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors
+
+//#include "log.h"
 
 unsigned int fp_control_word_std;
 unsigned int fp_control_word_muted;
 
-void fpExceptInit()
-{
-	//unsigned int was=_clear87();
-	//logPrintf("","fpExceptInit",LOG_INFO,"control87 status before clear was %08x", was);
-	fp_control_word_std = _control87(0, 0);             //4978 = 1001101110010
+
+void fpExceptInit()
+{
+	_controlfp_s(&fp_control_word_std, 0, 0); //in Visual C++, the default value is exactly the masks listed below, and we have to turn them off to enable exceptions
 	// Make the new fp env same as the old one, except for the changes we're going to make
-	fp_control_word_muted = fp_control_word_std | EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT;  //4991 = 1001101111111
-}
-
-void fpExceptEnable()
-{
-	unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception...
-	//logPrintf("","fpExceptEnable ",LOG_INFO,"control87 status before clear was %08x", was);
-	_control87(fp_control_word_std, 0xffffffff);
-	//logPrintf("","fpExceptEnable ",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
-}
-
-void fpExceptDisable()
-{
-	unsigned int was = _clear87(); //trzeba czyscic zeby nie bylo exception...
+	fp_control_word_muted = fp_control_word_std & ~(EM_INVALID | /*EM_DENORMAL |*/ EM_ZERODIVIDE | EM_OVERFLOW /* | EM_UNDERFLOW | EM_INEXACT */); //commented out exceptions that occur during proper operation
+}
+
+void fpExceptEnable()
+{
+	//_fpreset(); //not needed since we just _clearfp()... mentioned in https://stackoverflow.com/questions/4282217/visual-c-weird-behavior-after-enabling-floating-point-exceptions-compiler-b
+	unsigned int was = _clearfp(); //need to clean so that there is no exception...
+	//logPrintf("","fpExceptEnable",LOG_INFO,"control87 status before clear was %08x", was);
+	_controlfp_s(&was, fp_control_word_muted, _MCW_EM);
+}
+
+void fpExceptDisable()
+{
+	//_fpreset(); //not needed since we just _clearfp()... mentioned in https://stackoverflow.com/questions/4282217/visual-c-weird-behavior-after-enabling-floating-point-exceptions-compiler-b
+	unsigned int was = _clearfp(); //need to clean so that there is no exception...
 	//logPrintf("","fpExceptDisable",LOG_INFO,"control87 status before clear was %08x", was);
-	_control87(fp_control_word_muted, 0xffffffff);
-	//logPrintf("","fpExceptDisable",LOG_INFO,"control87 flags are %08x", _control87(0, 0)); //kontrola co sie ustawilo
-}
-
-#endif
-
-
-
-#ifdef _MSC_VER
-//Moznaby zrobic tak jak pod linuxem czyli wlaczyc exceptiony na poczatku i wylaczac na chwile przy dzieleniu w extvalue.
-//To by pozwoli³o na wy³apanie pod visualem z³ych sytuacji kiedy framsy licz¹ na NaN, INF itp.
-//http://stackoverflow.com/questions/2769814/how-do-i-use-try-catch-to-catch-floating-point-errors
-void fpExceptInit() {}
-void fpExceptEnable() {}
-void fpExceptDisable() {}
-#endif
+	_controlfp_s(&was, fp_control_word_std, _MCW_EM);
+}
+#endif
