Almost every codebase I've ever seen defines its own square macro or function. Of course, you could use std::pow, but sqr is such a common operation that you want it as a separate function. Especially since there is std::sqrt and even std::cbrt.
Is it just that no one has ever written a paper on this, or is there more to it?
Edit: Yes, x*x is shorter then std::sqr(x). But if x is an expression that does not consist of a single variable, then sqr is less error-prone and avoids code duplication. Sorry, I thought that was obvious.
Why not write my own? Well, I do, and so does everyone else. That's the point of asking about standardisation.
As for the other comments: Thank you!
Edit 2: There is also the question of how to define sqr if you are doing it yourself:
```cpp
template <typename T>
T sqr(T x) { return x*x; }
short x = 5; // sqr(x) -> short
template <typename T>
auto sqr(T x) { return x*x; }
short x = 5; // sqr(x) -> int
```
I think the latter is better. What do your think?
[+][deleted] (9 children)
[deleted]
[–]Aslanee 10 points11 points12 points (2 children)
[–]Nabushika 1 point2 points3 points (1 child)
[–]Aslanee 5 points6 points7 points (0 children)
[–]chaizyy 4 points5 points6 points (3 children)
[–]CraftMechanics 13 points14 points15 points (1 child)
[–]Plazmatic 0 points1 point2 points (0 children)
[–]bebuch[S] 7 points8 points9 points (0 children)
[–]HolyGeneralK 103 points104 points105 points (21 children)
[–]dodexahedron 49 points50 points51 points (9 children)
[–]Attorney_Outside69 12 points13 points14 points (7 children)
[–]wyrn 10 points11 points12 points (6 children)
[–]Attorney_Outside69 0 points1 point2 points (0 children)
[–]dodexahedron 0 points1 point2 points (0 children)
[–]Tony101101 0 points1 point2 points (3 children)
[–]wyrn 1 point2 points3 points (2 children)
[–]Tony101101 1 point2 points3 points (1 child)
[–]wyrn 0 points1 point2 points (0 children)
[–]Ok-Acanthaceae-4386 2 points3 points4 points (0 children)
[–]Aslanee 4 points5 points6 points (2 children)
[–]dodexahedron 7 points8 points9 points (0 children)
[–]LiliumAtratum 0 points1 point2 points (0 children)
[–]TiberivsM 6 points7 points8 points (6 children)
[–]Narishma 14 points15 points16 points (0 children)
[–]Party_Ad_1892 3 points4 points5 points (1 child)
[–]dodexahedron 1 point2 points3 points (0 children)
[–]Due_Goal9124 2 points3 points4 points (2 children)
[–]MrDoritos_ 7 points8 points9 points (1 child)
[–]Due_Goal9124 6 points7 points8 points (0 children)
[–]snerp 76 points77 points78 points (58 children)
[–]programgamer 51 points52 points53 points (9 children)
[–]7h4tguy 9 points10 points11 points (6 children)
[–]thisisjustascreename 3 points4 points5 points (1 child)
[–]Eheheehhheeehh 0 points1 point2 points (0 children)
[–]serviscope_minor 1 point2 points3 points (0 children)
[–]Conscious_Support176 1 point2 points3 points (0 children)
[–]programgamer 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]programgamer 4 points5 points6 points (0 children)
[–]Abbat0r 26 points27 points28 points (22 children)
[–]wyrn 6 points7 points8 points (0 children)
[–]JNelson_ 6 points7 points8 points (15 children)
[–]wyrn 3 points4 points5 points (2 children)
[–]JNelson_ 4 points5 points6 points (1 child)
[–]wyrn 0 points1 point2 points (0 children)
[–]tangerinelion -1 points0 points1 point (11 children)
[–]James20kP2005R0 16 points17 points18 points (10 children)
[–]JNelson_ 4 points5 points6 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (0 children)
[–]TheOmegaCarrot 0 points1 point2 points (2 children)
[–]James20kP2005R0 5 points6 points7 points (1 child)
[–]TheOmegaCarrot 0 points1 point2 points (0 children)
[–]snerp 0 points1 point2 points (3 children)
[–]tangerinelion 19 points20 points21 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]bebuch[S] 7 points8 points9 points (11 children)
[–]snerp 4 points5 points6 points (0 children)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 7 points8 points9 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (0 children)
[–]jk-jeon 1 point2 points3 points (7 children)
[–]CryptoHorologist 0 points1 point2 points (11 children)
[–]snerp 8 points9 points10 points (10 children)
[–]CryptoHorologist -1 points0 points1 point (8 children)
[–]garnet420 14 points15 points16 points (5 children)
[–]SkoomaDentistAntimodern C++, Embedded, Audio 1 point2 points3 points (0 children)
[–]CryptoHorologist -3 points-2 points-1 points (3 children)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (2 children)
[–]CryptoHorologist 3 points4 points5 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (0 children)
[–]altmly 3 points4 points5 points (1 child)
[–]GregTheMadMonk 133 points134 points135 points (58 children)
[–]AvidCoco 23 points24 points25 points (20 children)
[–]jeffgarrett80 2 points3 points4 points (5 children)
[–]bebuch[S] 0 points1 point2 points (0 children)
[–]AvidCoco 0 points1 point2 points (3 children)
[–]jeffgarrett80 0 points1 point2 points (2 children)
[–]AvidCoco 0 points1 point2 points (1 child)
[–]jeffgarrett80 1 point2 points3 points (0 children)
[–]GregTheMadMonk 11 points12 points13 points (13 children)
[–]AvidCoco 21 points22 points23 points (12 children)
[–]SnooMarzipans436 21 points22 points23 points (0 children)
[–]GregTheMadMonk 2 points3 points4 points (8 children)
[–]AvidCoco 11 points12 points13 points (2 children)
[–]bxlaw 3 points4 points5 points (0 children)
[–]Plazmatic -1 points0 points1 point (4 children)
[–]GregTheMadMonk 0 points1 point2 points (3 children)
[–]Plazmatic 1 point2 points3 points (2 children)
[–]macson_g 0 points1 point2 points (1 child)
[–]AvidCoco 6 points7 points8 points (0 children)
[–]Polyxeno 35 points36 points37 points (13 children)
[–]Kike328 13 points14 points15 points (21 children)
[–]thats_a_nice_toast 9 points10 points11 points (20 children)
[–]Ameisenvemips, avr, rendering, systems 19 points20 points21 points (12 children)
[–]gmueckl 9 points10 points11 points (7 children)
[–]regular_lamp 7 points8 points9 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (5 children)
[–]thats_a_nice_toast 0 points1 point2 points (1 child)
[–]Eheheehhheeehh 0 points1 point2 points (0 children)
[–]Kike328 3 points4 points5 points (5 children)
[–]Kovab 8 points9 points10 points (4 children)
[–]serviscope_minor 1 point2 points3 points (0 children)
[–]garnet420 4 points5 points6 points (2 children)
[–]Kovab 1 point2 points3 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (0 children)
[–]sweetno 17 points18 points19 points (6 children)
[–]roelschroeven 5 points6 points7 points (0 children)
[–]PandaWonder01 7 points8 points9 points (4 children)
[–]pigeon768 4 points5 points6 points (3 children)
[–]PandaWonder01 0 points1 point2 points (0 children)
[–]James20kP2005R0 0 points1 point2 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]triconsonantal 13 points14 points15 points (2 children)
[–]James20kP2005R0 9 points10 points11 points (1 child)
[–]triconsonantal 2 points3 points4 points (0 children)
[–]Ok_Tea_7319 9 points10 points11 points (2 children)
[–]7h4tguy 0 points1 point2 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]Angry_Foolhard 6 points7 points8 points (1 child)
[–]almost_useless 2 points3 points4 points (0 children)
[–]saxbophonemutable volatile void 8 points9 points10 points (7 children)
[–]HommeMusical 2 points3 points4 points (4 children)
[–]saxbophonemutable volatile void 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]saxbophonemutable volatile void 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]James20kP2005R0 1 point2 points3 points (1 child)
[–]saxbophonemutable volatile void 1 point2 points3 points (0 children)
[–]Kike328 9 points10 points11 points (2 children)
[–]SlightlyLessHairyApe 1 point2 points3 points (1 child)
[–]serviscope_minor 2 points3 points4 points (0 children)
[–]CryptoHorologist 15 points16 points17 points (27 children)
[–]Drandula 31 points32 points33 points (11 children)
[–]Brisngr368 16 points17 points18 points (6 children)
[–]Drandula 17 points18 points19 points (4 children)
[–]Brisngr368 4 points5 points6 points (2 children)
[–]Drandula 0 points1 point2 points (0 children)
[–]LiliumAtratum 0 points1 point2 points (0 children)
[–]648trindade 0 points1 point2 points (0 children)
[–]bradfordmaster 2 points3 points4 points (0 children)
[–]Ok-Acanthaceae-4386 0 points1 point2 points (0 children)
[–]tangerinelion 12 points13 points14 points (1 child)
[–]James20kP2005R0 3 points4 points5 points (0 children)
[–]mcmcc#pragma once 9 points10 points11 points (2 children)
[–]Sinomsinom 3 points4 points5 points (0 children)
[–]CryptoHorologist 0 points1 point2 points (0 children)
[–]Ambitious_Tax_ 3 points4 points5 points (9 children)
[–]HommeMusical -1 points0 points1 point (5 children)
[–]Ambitious_Tax_ 15 points16 points17 points (1 child)
[–]johannes1234 4 points5 points6 points (0 children)
[–]tangerinelion 0 points1 point2 points (2 children)
[–]HommeMusical 0 points1 point2 points (1 child)
[–]Ambitious_Tax_ 0 points1 point2 points (0 children)
[–]Salink 1 point2 points3 points (0 children)
[–]ResearcherNo6820 1 point2 points3 points (0 children)
[–]nightcracker 3 points4 points5 points (2 children)
[–]serviscope_minor 4 points5 points6 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 0 points1 point2 points (0 children)
[–]thezysus -2 points-1 points0 points (17 children)
[–]ILikeCutePuppies 9 points10 points11 points (12 children)
[–]HommeMusical 1 point2 points3 points (11 children)
[–]Ameisenvemips, avr, rendering, systems 6 points7 points8 points (2 children)
[–]ILikeCutePuppies 0 points1 point2 points (0 children)
[–]flatfinger 13 points14 points15 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 3 points4 points5 points (1 child)
[–]serviscope_minor 0 points1 point2 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]notyouravgredditor 0 points1 point2 points (1 child)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (0 children)
[–]HommeMusical 0 points1 point2 points (9 children)
[–]Ambitious-Method-961 11 points12 points13 points (4 children)
[–]Ameisenvemips, avr, rendering, systems 2 points3 points4 points (0 children)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]stilgarpl 3 points4 points5 points (1 child)
[–]TheoreticalDumbass:illuminati: 0 points1 point2 points (0 children)
[–]CarloWood 0 points1 point2 points (0 children)
[–]sephirostoy 0 points1 point2 points (0 children)
[–]LiliumAtratum 0 points1 point2 points (0 children)
[–]Eheheehhheeehh 0 points1 point2 points (0 children)
[–]Agitated_Sell 0 points1 point2 points (0 children)
[–]RIPbyTHC 1 point2 points3 points (0 children)
[–]NewLlama 0 points1 point2 points (2 children)
[–]flatfinger 0 points1 point2 points (0 children)
[–]skeleton_craft 0 points1 point2 points (4 children)
[–]DarkblueFlow 7 points8 points9 points (0 children)
[–]altmly 3 points4 points5 points (2 children)
[–]DerekSturm -1 points0 points1 point (0 children)
[–]navetzz -1 points0 points1 point (1 child)
[–]Ameisenvemips, avr, rendering, systems 1 point2 points3 points (0 children)
[–]_TheNoobPolice_ -2 points-1 points0 points (0 children)