1

The c-style casting (int)(x) is producing different results for different platforms when x is float.

I want to change all the instances of (int)(x) with a custom function, lets say CustomCast(x) and I want to do that only if x is float.

Is there any easier way than to go through all the files one by one and changing it?

  • What will your CustomCast do differently to ensure identical results? – Neil Kirk Mar 25 '15 at 05:01
  • 1
    If you are willing to go out of your comfort zone, you could use [sed for win32](http://gnuwin32.sourceforge.net/packages/sed.htm) – dvhh Mar 25 '15 at 05:15
  • @Neil Kirk (int) is supposed to truncate anything after decimal point. However in some cases it rounds up. For instance (int)1.98 gives me 2 which is incorrect. I guess the correct term for this is floating point drift. My CustomCast will use floor and then do a (int) cast which seems to work well for all platforms. – CatchMeIfYouTry Mar 25 '15 at 05:53
  • 1
    @CatchMeIfYouTry - I'm not getting the same results as you report. Which platform/tool chain is being used? I'm not really sure but perhaps the floating rounding mode could be the answer. See http://stackoverflow.com/questions/6867693/change-floating-point-rounding-mode – Support Ukraine Mar 25 '15 at 07:25
  • @nielsen This happens in certain embedded ARM boards under windows CE that have FPUs. It does not happen in windows. Also, thanks for the link. It looks like I could use that. – CatchMeIfYouTry Mar 25 '15 at 12:54
  • 1
    @CatchMeIfYouTry - I don't have much experience with ARM and FPU. But turning (int)1.98 into 2 sounds strange - more like a bug. If you did `float x = 1.9999999999999; int i = (int)x;` then 2 could/would be the correct result. Due to the limit precision of floats 1.9999999999999 could/would be rounded to 2 already at compile time. However, I'm not sure if all compilers would do it the same way. – Support Ukraine Mar 25 '15 at 16:47

0 Answers0