C++ Math

How to do square of a number in C++?
1
2
double number = 4.25;
double square = number * number;
or better way
use math.h header file
and use sq(number); as a function...
there are many functions like trigo and square root....google them out
closed account (zb0S216C)
@gauravchauhan9: "cmath" is C++; "math.h" is C.

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
template <typename T>  inline const volatile T square(T input) throw() { 
    register T* output;
    for (auto i = 0; i < 2; i++) {
        switch(i) {
        case 0:   output  = new T(input); break;
        default: *output *= input;     continue;
        }
    }
    return *output;
}

int main(int argc, char* argv[])
{
    try     {
        static signed short d = float(2);
        cout << square( static_cast<double>(d) );
    }
    catch(unsigned long i)    {
    }
}


I need ideas about how to stick more keywords in there (bonus points if you can fix the memory leak).
Last edited on
sqrt() would be best ...
Sikander Hayyat wrote:
sqrt() would be best ...

Wow. Are you absolutely sure about that?
Absolutely ...
Topic archived. No new replies allowed.