Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:49:46

0001 // name=rounding_nearbyint_test ; gcc $name.cc -std=c++11 -lstdc++ -I.. -I/usr/local/cuda/include -o /tmp/$name && /tmp/$name
0002 
0003 /**
0004 
0005 https://en.cppreference.com/w/cpp/numeric/math/nearbyint
0006 
0007 https://en.cppreference.com/w/cpp/numeric/math/rint
0008 
0009 **/
0010 
0011 #include <cassert>
0012 #include <cstdio>
0013 #include <cmath>
0014 #include <iostream>
0015 
0016 
0017 void test_char()
0018 {
0019     // char can hold values from -128 to 127  (char cannot hold 128, that would wrap to -128)
0020     for(int i=-128 ; i < 128 ; i++) printf("// i %d  (char) %d \n", i, int((char)i) );   
0021 }
0022 
0023 void test_lrint()
0024 {
0025     // compressing a value known to be in range -1.f -> 1.f (eg polarization component)
0026     float df = 1.f/127.f ; 
0027     unsigned count = 0 ;  
0028     for(float f=-1.f ; f <= 1.f ; f+=df )
0029     {
0030         float ff = (f+1.f)*127.f ; 
0031         long l = lrint(ff) ;  
0032         unsigned char uc = l ; 
0033         printf("// count %3d  f %10.4f  ff %10.4f  l %3ld uc %3d  \n", count, f,ff, l, uc  ); 
0034         assert( count == unsigned(l) ); 
0035         count += 1 ; 
0036     } 
0037     printf("// df %10.4f \n", df ); 
0038 }
0039 
0040 int main()
0041 {
0042     test_lrint(); 
0043     return 0 ; 
0044 }