Jump to content

  • Log In with Google      Sign In   
  • Create Account


which is fastest way to inverse a number in c++?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 thedodgeruk   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 January 2011 - 11:21 AM

not sure if there is a function for this



if its a positive , it makes it a negitive
if its a negitive then it makes it a positive



float num = 97.45;
num *=-1;

Sponsor:

#2 SamLowry   Members   -  Reputation: 1576

Like
0Likes
Like

Posted 16 January 2011 - 11:38 AM

x = -x; ?



#3 kilah   Members   -  Reputation: 397

Like
0Likes
Like

Posted 16 January 2011 - 12:04 PM

I would say its straight:
-num.

Is this worthy to worry about? I mean I've just done a test, and performance wise, difference are barely imposible to notice (testing on 1024^3 gave me ~1.10e-005 vs ~1.05e-005)

#4 iMalc   Crossbones+   -  Reputation: 2260

Like
1Likes
Like

Posted 16 January 2011 - 12:19 PM

not sure if there is a function for this



if its a positive , it makes it a negitive
if its a negitive then it makes it a positive



float num = 97.45;
num *=-1;

That's called "negating", not "inversing", and yes x = -x is the answer.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#5 CodeDemon   Members   -  Reputation: 363

Like
1Likes
Like

Posted 16 January 2011 - 12:28 PM

If you've got a lot of numbers you need to negate, you can batch negate them using whatever SIMD hardware you have at your disposal.

For example, with SSE:

size_t const count = 10000;
__declspec(align(16)) float values[count] = { ... }; // values to negate

__m128 zero = _mm_set_ps1(0.0f);
float* p = values;

// negate 16 values every iteration
for (size_t i = 0, n = count / 16; i < n; p += 16, ++i) {
	__m128 a = _mm_load_ps(p);
	__m128 b = _mm_load_ps(p+4);
	__m128 c = _mm_load_ps(p+8);
	__m128 d = _mm_load_ps(p+12);
	a = _mm_sub_ps(zero, a);
	b = _mm_sub_ps(zero, <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
	c = _mm_sub_ps(zero, c);
	d = _mm_sub_ps(zero, d);
	_mm_store_ps(p, a);
	_mm_store_ps(p+4, <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
	_mm_store_ps(p+8, c);
	_mm_store_ps(p+12, d);
}

// negate remaining values
for (float* p_end = values + count; p < p_end; ++p) {
	*p = -(*p);
}

Of course, that's probably overkill.

#6 D.Chhetri   Members   -  Reputation: 181

Like
0Likes
Like

Posted 16 January 2011 - 04:35 PM

You can check the sign bit and inverse it if necessary, but you have other things to worry about, than negating numbers don't you?
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

#7 Mantear   Members   -  Reputation: 251

Like
0Likes
Like

Posted 16 January 2011 - 09:42 PM

Keep it simple. x = -x; Let the compiler perform any tricks required to make it fast. Worry about making your program run correctly before worrying about making it run fast. If you think this operation is too slow, profile it. Only then worry about it if it's truly a bottleneck (it shouldn't be).

#8 haegarr   Crossbones+   -  Reputation: 3778

Like
0Likes
Like

Posted 17 January 2011 - 03:13 AM

...
That's called "negating", not "inversing", and yes x = -x is the answer.

A bit nitpicking: You're right: The usual name is negation. But on the other hand, negation is a specialization of the inverse with additive math in mind. See e.g. here on wikipedia. This is a similar distinction as with the neutral element, what is 0 for addition and 1 for multiplication.

#9 Rattenhirn   Crossbones+   -  Reputation: 1665

Like
0Likes
Like

Posted 17 January 2011 - 10:56 AM

Most FPUs have an instruction to change the sign of a floating point number. That would be the fastest way, but isn't very portable...

#10 Antheus   Members   -  Reputation: 2393

Like
0Likes
Like

Posted 17 January 2011 - 02:08 PM

This article outlines several other methods.

#11 V-man   Members   -  Reputation: 801

Like
0Likes
Like

Posted 17 January 2011 - 02:22 PM

Yes but that article, that function sets up some other variables and thus it is slower.
The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#12 way2lazy2care   Members   -  Reputation: 782

Like
0Likes
Like

Posted 17 January 2011 - 03:26 PM


...
That's called "negating", not "inversing", and yes x = -x is the answer.

A bit nitpicking: You're right: The usual name is negation. But on the other hand, negation is a specialization of the inverse with additive math in mind. See e.g. here on wikipedia. This is a similar distinction as with the neutral element, what is 0 for addition and 1 for multiplication.


I'd agree that negating is not inverting at least as far as programming is concerned. One's compliment could easily be interpreted as the "inverse" of a number in a program, which is not the same as the negation of a number.

edit:

" 'inverse' is a noun. The verb is 'invert'. The 'inverse' is the result of inverting."

oh god. You'd think with a browser that underlines everything in red people (including me) would catch such things.
/shame on family

#13 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 17 January 2011 - 05:48 PM

Aarrgh.

'inverse' is a noun. The verb is 'invert'. The 'inverse' is the result of inverting.

#14 Hodgman   Moderators   -  Reputation: 27879

Like
1Likes
Like

Posted 17 January 2011 - 07:25 PM


This article outlines several other methods.

Yes but that article, that function sets up some other variables and thus it is slower.
The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

No it should be done in the cloud with HTML and variables.

#15 deepdene   Members   -  Reputation: 292

Like
0Likes
Like

Posted 17 January 2011 - 09:08 PM


This article outlines several other methods.

Yes but that article, that function sets up some other variables and thus it is slower.
The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

No it should be done in the cloud with HTML and variables.


:) If you believe the buzz you could almost believe that.




#16 Hodgman   Moderators   -  Reputation: 27879

Like
0Likes
Like

Posted 18 January 2011 - 01:46 AM



This article outlines several other methods.

Yes but that article, that function sets up some other variables and thus it is slower.
The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

No it should be done in the cloud with HTML and variables.

:) If you believe the buzz you could almost believe that.

I believe every gd.net meme, and anything posted on dailyWTF...

#17 agentleo   Members   -  Reputation: 173

Like
0Likes
Like

Posted 18 January 2011 - 01:56 AM


This article outlines several other methods.

Yes but that article, that function sets up some other variables and thus it is slower.
The fastest way to batch process is to use MMX for integers and SSE for floats using the CPU.

The GPU way would be way faster. Use CUDA or OpenCL.

No it should be done in the cloud with HTML and variables.


made me chuckle. All this hype about the cloud can get pretty silly.
Just a wannabe grinding in this game we call Life.

#18 The Communist Duck   Members   -  Reputation: 154

Like
0Likes
Like

Posted 18 January 2011 - 11:57 AM

I fail to see how x = -x could possibly be a bottleneck. Unless you were developing in a language (written in a language) repeat previous about 6 times - written in Python.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS