• 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.

17 replies to this topic

### #1thedodgeruk  Members

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;

### #2SamLowry  Members

1865
Like
0Likes
Like

Posted 16 January 2011 - 11:38 AM

x = -x; ?

### #3kilah  Members

513
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)

### #4iMalc  Members

2466
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

### #5CodeDemon  Members

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) {
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.

### #6D.Chhetri  Members

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?
Visit my site
Visit my github

### #7Mantear  Members

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).

### #8haegarr  Members

7190
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.

### #9Rattenhirn  Members

2901
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...

### #10Antheus  Members

2409
Like
0Likes
Like

Posted 17 January 2011 - 02:08 PM

### #11V-man  Members

813
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];
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

### #12way2lazy2care  Members

786
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

### #13Zahlman  Members

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.

### #14Hodgman  Moderators

49430
Like
1Likes
Like

Posted 17 January 2011 - 07:25 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.

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

### #15deepdene  Members

292
Like
0Likes
Like

Posted 17 January 2011 - 09:08 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.

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

If you believe the buzz you could almost believe that.

### #16Hodgman  Moderators

49430
Like
0Likes
Like

Posted 18 January 2011 - 01:46 AM

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...

### #17agentleo  Members

173
Like
0Likes
Like

Posted 18 January 2011 - 01:56 AM

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.

### #18The Communist Duck  Members

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.