Finding a square root
This is probably a basic C++ question, but I''m a beginner and I lost my durn MSDN Library cd. So I can''t look this stuff up. Anyway, how do you find a square root of a number? And how do you square a number? I''ve tried:
x^2 to square it, and
x^(1/2) to find the square root, but they don''t work. My guess is that the ^ character doesn''t have anything to do with exponents, I remember reading something about it being a not symbol, or something like that. Anyway, how do you find the square root of a number, and how do you raise numbers to exponents? Also, what files must I include to use these functions?
John Licatogamecreationlab.com
The "^" operator is bitwise XOR.
For square roots you want to use double sqrt(double x) function. For exponents you''d want to use the double pow(double x, double y) function. They are both in "math.h".
-------
Andrew
For square roots you want to use double sqrt(double x) function. For exponents you''d want to use the double pow(double x, double y) function. They are both in "math.h".
-------
Andrew
For powers of two you can use shifting:
For x^2:
x<<1;
For x^4:
x<<2;
For x^8:
x<<3;
Et cetera.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
For x^2:
x<<1;
For x^4:
x<<2;
For x^8:
x<<3;
Et cetera.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
That''s wrong. X << 1 is the same as X * 2, not X to the power of 2 (or X * X). More generally:
X << N == X * 2^N
X >> N == X / 2^N
~CGameProgrammer( );
X << N == X * 2^N
X >> N == X / 2^N
~CGameProgrammer( );
~CGameProgrammer( );
Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
As a side note can still use shifting for powers of 2, though; just do it the other way around:
2^n == 1 << n
2^n == 1 << n
--Ben CarterRomans 8:38-39
As a side note, you can still use shifting for powers of 2, though; just do it the other way around:
2^n == 1 << n
2^n == 1 << n
--Ben CarterRomans 8:38-39
You can if the number is a perfect square and a multiple of two
.
Martee
Magnum Games.NET
All your code are belong to us.
![](smile.gif)
Martee
Magnum Games.NET
All your code are belong to us.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Yeah, I don''t know what I was thinking there
. I was off on some weird tangent of thought
.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
![](wink.gif)
![](tongue.gif)
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
You can use shifts to calculate square roots...
![](tongue.gif)
|
![](tongue.gif)
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement