Skip to main content
GameDev.net gamedev.net
🔒 Locked

the lost code .. rint()

Started by kickkick Aug 27, 2006 at 8:04 AM 21 replies 4.8k views
Original Post
kickkick
kickkick
Where can i find the rint() function for C? It should be declared in but it's not. [using MSVCE]
kickkick
kickkick
rint() should be a function that clamps a floating point value to the nearest integer value.
kickkick
kickkick
exactly
kickkick
kickkick
actually, that's only for positive values

EDIT: someone was faster
kickkick
kickkick
so uh... anyone knows this rint()?
kickkick
kickkick
maybe.

what functions are in math.h that do this?
Shadowdancer
Shadowdancer
Quote from the rint manpage: "The rint() function conforms to 4.3BSD.", so no ANSI/ISO-C.
Deyja
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.
CTar
CTar
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-->

Is 105% faster. It is just a simple test, if you are interreseted it looks as follows (it saves everything to a discard.txt file so that the compiler won't just delete the tests):
#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;}


[Edited by - CTar on August 27, 2006 3:26:58 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.