• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
thedodgeruk

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

17 posts in this topic

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;
0

Share this post


Link to 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)
0

Share this post


Link to post
Share on other sites
[quote name='thedodgeruk' timestamp='1295198497' post='4759683']
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;
[/quote]That's called "negating", not "inversing", and yes x = -x is the answer.
1

Share this post


Link to 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:

[code]
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, B);
c = _mm_sub_ps(zero, c);
d = _mm_sub_ps(zero, d);
_mm_store_ps(p, a);
_mm_store_ps(p+4, 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);
}
[/code]

Of course, that's probably overkill.
1

Share this post


Link to 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?
0

Share this post


Link to 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).
0

Share this post


Link to 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.
[/quote]
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. [url="http://en.wikipedia.org/wiki/Inverse_element"]here on wikipedia[/url]. This is a similar distinction as with the neutral element, what is 0 for addition and 1 for multiplication.
0

Share this post


Link to post
Share on other sites
This [url="http://thedailywtf.com/Articles/Classic-WTF-The-Challenges-of-Negation.aspx"]article[/url] outlines several other methods.
0

Share this post


Link to 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.
0

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1295255634' post='4760010']
[quote name='iMalc' timestamp='1295201972' post='4759709']
...
That's called "negating", not "inversing", and yes x = -x is the answer.
[/quote]
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. [url="http://en.wikipedia.org/wiki/Inverse_element"]here on wikipedia[/url]. 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
0

Share this post


Link to post
Share on other sites
Aarrgh.

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

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1295295752' post='4760319'] [quote name='Antheus' timestamp='1295294888' post='4760307']
This [url="http://thedailywtf.com/Articles/Classic-WTF-The-Challenges-of-Negation.aspx"]article[/url] outlines several other methods.
[/quote]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.
1

Share this post


Link to 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 [url="http://thedailywtf.com/Articles/Classic-WTF-The-Challenges-of-Negation.aspx"]article[/url] outlines several other methods.
[/quote]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.


0

Share this post


Link to post
Share on other sites
[quote name='deepdene' timestamp='1295320081' post='4760518']
[quote name='Hodgman' timestamp='1295313924' post='4760482']
[quote name='V-man' timestamp='1295295752' post='4760319'] [quote name='Antheus' timestamp='1295294888' post='4760307']
This [url="http://thedailywtf.com/Articles/Classic-WTF-The-Challenges-of-Negation.aspx"]article[/url] outlines several other methods.
[/quote]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...
0

Share this post


Link to 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 [url="http://thedailywtf.com/Articles/Classic-WTF-The-Challenges-of-Negation.aspx"]article[/url] outlines several other methods.
[/quote]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.
0

Share this post


Link to 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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0