• Advertisement

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

This topic is 2563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Advertisement
x = -x; ?

Share this post

Share on other sites
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)

Share this post

Share on other sites

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.

Share this post

Share on other sites
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, ; c = _mm_sub_ps(zero, c); d = _mm_sub_ps(zero, d); _mm_store_ps(p, a); _mm_store_ps(p+4, ; _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.

Share this post

Share on other sites
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?

Share this post

Share on other sites
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).

Share this post

Share on other sites

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

Share this post

Share on other sites
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...

Share this post

Share on other sites
This article outlines several other methods.

Share this post

Share on other sites
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.

Share this post

Share on other sites

[quote name='iMalc' timestamp='1295201972' post='4759709']
...
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.
[/quote]

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

Share this post

Share on other sites
Aarrgh.

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

Share this post

Share on other sites
[quote name='Antheus' timestamp='1295294888' post='4760307']
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.[/quote]No it should be done in the cloud with HTML and variables.

Share this post

Share on other sites

[quote name='V-man' timestamp='1295295752' post='4760319'] [quote name='Antheus' timestamp='1295294888' post='4760307']
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.[/quote]No it should be done in the cloud with HTML and variables.
[/quote]

If you believe the buzz you could almost believe that.

Share this post

Share on other sites

[quote name='Hodgman' timestamp='1295313924' post='4760482']
[quote name='V-man' timestamp='1295295752' post='4760319'] [quote name='Antheus' timestamp='1295294888' post='4760307']
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.[/quote]No it should be done in the cloud with HTML and variables.
[/quote] If you believe the buzz you could almost believe that.[/quote]I believe every gd.net meme, and anything posted on dailyWTF...

Share this post

Share on other sites

[quote name='V-man' timestamp='1295295752' post='4760319'] [quote name='Antheus' timestamp='1295294888' post='4760307']
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.[/quote]No it should be done in the cloud with HTML and variables.
[/quote]

made me chuckle. All this hype about the cloud can get pretty silly.

Share this post

Share on other sites
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.

Share this post

Share on other sites

• Advertisement
• Advertisement
• Popular Tags

• Advertisement
• Popular Now

• 11
• 21
• 10
• 11
• 12
• Advertisement