Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22192

Cast float to int with consistent results accross platforms/compilers/cpu archs [closed]

$
0
0

I wrote a program to do some float caculation and cast the result to int; When I submit the job to the cluster multiple times, I got different result even when I provided same parameters;

  // compiled with gcc8.3, centos  float* x = ....;  float* y = ....;  // simply  use [+ * - /] to caculate cos simlilarity  float similarity = cos_similiarity(x, y, dim);  similarity  = similarity * 100;  int result = std::round(similarity);

I cast a float (range 0 - 1) to int (with multiplier 100); Sometimes I get unstable result, like 50 and 49; I guess the float represention of cos_sim function result maybe 49.50...01 and 49.499...9 when running on different machines, so the round function rounds to different int;And I have following question:

  1. Is my guess right? Same binary(asm code) can have different float math result running on different machines/platforms/libc versions..?
  2. If so, how to make my result consistent?

Viewing all articles
Browse latest Browse all 22192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>