Original Post
Where can i find the rint() function for C? It should be declared in but it's not. [using MSVCE]
Quote:
This is ok for values greater than zero, but fails for others.
-11.9 + 0.5 = 11
Quote:
Original post by Anonymous Poster Quote:
Original post by Deyja Quote:
This is ok for values greater than zero, but fails for others.
-11.9 + 0.5 = 11
(int)(floorf(float_value + 0.5f))
Now -11.9 is properly rounded down to -12.
-11.9 + 0.5 = -11.4 floor that and you have 11.
Out of curiosity I have tested and profiled a none assembly function and the following is the quickest I have been able to do.
*** Source Snippet Removed ***
Can anybody see a quicker none assembly version.(not these were not far off the asm code)
Your version is quite slow, my test shows that the following:inline int round2(float f)
{
return static_cast<int>(f+((f<0)?-0.5F:0.5F));
}<!--QUOTE-->
#include <iostream>#include <windows.h>#include <fstream>//C++ this suprised me as a ref was quicker than a pointerinline void round1(int& i, float f) { if(f>0.0f) f - (int)(f) > 0.50f ? i =(int)(f+0.5f) : i=(int)f; else f - (int)(f) < 0.50f ? i =(int)(f-0.5f) : i=(int)f;}inline int round2(float f){ return static_cast<int>(f+((f<0)?-0.5F:0.5F));}int main(){ std::ofstream out("discard.txt"); int data[100100]; int counter = -1; __int64 freq,t0,t1; QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER*>(& freq) ); QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>(& t0) ); for( float i = -5000.0F;i<5000.0F;i+=0.1F ) { round1(data[++counter],i); } QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>(& t1) ); double time0=(t1-t0)/static_cast<long double>(freq); std::cout << time0 << "\n"; for( int i = 0;i<counter;++i) { out << data; } counter = -1; QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>(& t0) ); for( float i = -5000.0F;i<5000.0F;i+=0.1F ) { data[++counter] = round2(i); } QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>(& t1) ); double time1=(t1-t0)/static_cast<long double>(freq); std::cout << time1 << "\n"; std::cout << "Improvement: " << (time0/time1)-1.0 << "\n"; for( int i = 0;i<counter;++i) { out << data; } std::cin.get(); return 0;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more