• 0

math question in C++


Question

19 answers to this question

Recommended Posts

  • 0

PI is 22/7, if you're taking AP Computer Science you should get into the habit of declaring it as a constant.

<pre>const double PI=22/7;</pre>

To square a number, just multiply it by itself. Let's say x=5

<pre>x*x (returns 25)</pre>

if you want to get a square root, you need to #include and use the sqrt(); function.

<pre>sqrt(25); //returns 5</pre>

Hope that helps!

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464067
Share on other sites

  • 0
  Quote
Originally posted by JZolloXP

PI is 22/7, if you're taking AP Computer Science you should get into the habit of declaring it as a constant.

<pre>const int PI=22/7;</pre>

To square a number, just multiply it by itself. Let's say x=5

<pre>x*x (returns 25)</pre>

if you want to get a square root, you need to #include and use the sqrt(); function.

<pre>sqrt(25); //returns 5</pre>

Hope that helps!

22/7 is not pi, it may have the first 3 digits correct, but the rest are all wrong. Thought 22/7 is regarded as computer programmers, and I am astounded on why they chose such an inaccurate ratio, as in computer science even having a few digits off could freeze up everything.(ie recursive loops).

At least 52163/16604 is accurate to 7 digits :) But to truely find pi, you must create a program for it :)(hint: the program will be a recursive program)

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464072
Share on other sites

  • 0
  Quote
Not sure about pi, but for squaring a number put "^ 2" after it w/o quotes.

I just tried doing that (i didn't know ^ was an exponet operator) and it wouln't work...here's my code

<pre>

#include <iostream.h>

#include <math.h>



void main()

{

	cout << (4^2) << endl;

}

</pre>

It returns 6 for some odd reason :paranoid:

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464094
Share on other sites

  • 0
  Quote
Originally posted by JZolloXP

I just tried doing that (i didn't know ^ was an exponet operator) and it wouln't work...here's my code

<pre>

#include <iostream.h>

#include <math.h>



void main()

{

	cout << (4^2) << endl;

}

</pre>

It returns 6 for some odd reason :paranoid:

Odd indeed......should be exponent but it performs the operation as if 4 and 2 were being added. :ponder:

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464102
Share on other sites

  • 0
  Quote
Originally posted by JZolloXP

I just tried doing that (i didn't know ^ was an exponet operator) and it wouln't work...here's my code

<pre>

#include <iostream.h>

#include <math.h>



void main()

{

	cout << (4^2) << endl;

}

</pre>

It returns 6 for some odd reason :paranoid:

wooohooo my c interpreter is way different from yours :) I have no idea what cout and :p)

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464106
Share on other sites

  • 0
  Quote
Originally posted by DeathLace

the program can just be this for squaring pie:

#include

#include

void main()

{

const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808;

cout << pi^2 << endl;

}

Maybe it's just me but the ^ operator isn't working at all, when I compile the above program it gives me this error

error C2297: '<<' : illegal, right operand has type 'class ostream &(__cdecl *)(class ostream &)'

When I take out the ^, everything works fine. I'm using Visual C++ 6 in case you're wondering...

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464130
Share on other sites

  • 0
  Quote
Originally posted by DeathLace

the program can just be this for squaring pie:

#include

#include

void main()

{

const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808;

cout << pi^2 << endl;

}

ooooh, now you're really calculating pi....hmmmm somehow I consider that cheating :p Oh well :)

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464134
Share on other sites

  • 0

Seems to me if you do this:

const double pi =

3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808;

you're going to run into a lot of floating point inaccuracies. How many digits do you really need to be accurate to? If you use say 8-10 digits of precision it should probably be enough.

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464787
Share on other sites

  • 0
  Quote
Originally posted by Tesseract

Not sure about pi, but for squaring a number put "^ 2" after it w/o quotes.

Where did you learn how to program? The ^ operator is reserved for XOR

the easiest way to raise something to a power is to use the pow() function

pow(4,2); is the same as 4^2 on a calculator

i think you need to include math.h

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464806
Share on other sites

  • 0
  Quote
Originally posted by Evolution

hehehe I just found a pi program off the internet :p Now this is great ;)

http://remus.rutgers.edu/~rhoads/Obfuscated_C/pi.c

hahaha, well at least I'll give him creativity marks :)

That is mild compared to a lot of the programs in the obfuscated C contest

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-464809
Share on other sites

  • 0
  Quote
Originally posted by xWeston

Where did you learn how to program? The ^ operator is reserved for XOR

the easiest way to raise something to a power is to use the pow() function

pow(4,2); is the same as 4^2 on a calculator

i think you need to include math.h

I agree, you can't use 4^2 to represent 4 square.

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-465450
Share on other sites

  • 0

I've never seen ^ used for squaring numbers except on graphing calculators. I've always just used pow(#,#); whenever I needed a power done. And I'm not sure, but with Visual Studio 6 I think there's a .h file that MS included with the package that has pi calculated out to the right number of digits for accurate floats.

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-465471
Share on other sites

  • 0

  1. Very fewcomputers use more 16 digits to represent a double. You don't need to put 60 digits in your const definition.
  2. An easy way to calculate pi would be to use the atan function. Remember your H.S. geometry? tan( pi ) == 1/4. This function is easier than memorizing 16 (or 60!) digits of pi. Plus, it also provides you with the most accurate representation of pi for the machine you are working on.

&lt;pre&gt;

#include &lt;iostream&gt;

#include &lt;cmath&gt;



using namespace std;



int main()

{

   const double pi = 4.0 * atan( 1.0 );

   cout &lt;&lt; "Pi = " &lt;&lt; pi &lt;&lt; endl;

   return 0;

}

&lt;/pre&gt;

This code will work for compilers that conform to the C++ standard. I used g++ 3.2 to test this piece of code.

-kt

  Quote
Originally posted by DeathLace

the program can just be this for squaring pie:

#include

#include

void main()

{

const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808;

cout << pi^2 << endl;

}

Link to comment
https://www.neowin.net/forum/topic/45924-math-question-in-c/#findComment-465584
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Higher "Social Charges". I think that is a feel good phrase for 'Employee Deferred Compensation Charges". Then again, they use different wording in the "Old World." More precisely in American Accounting Lingo: "Mark to the Market Employee Differed Compensation Charges."
    • TikTok's "Add to Music App" feature gets support for another music streaming service by David Uzondu Image via Depositphotos.com TikTok's "Add to Music App" feature is getting support for another streaming service: YouTube Music. This comes a few months after the short-form video giant brought the feature to SoundCloud. "Add to Music App", if you have not heard of it, is a feature launched back in November 2023, initially for US and UK users, that makes saving music a lot easier. With this feature, users get an "Add Song" button next to the track name at the bottom of a TikTok video. When a customer first uses the feature, they can select their preferred streaming service from the available options, and this choice then becomes the default for all future one-tap saves (this can be changed anytime in the app's settings). The new YouTube Music integration means you can directly save the track to the streaming service with a single press. Users can also add a track from an artist's Sound Detail Page. When "Add to Music App" first appeared, the options were limited to Spotify and Amazon Music. Since then, TikTok has added Apple Music and Deezer. Each service gets a designated spot for the saved tracks, like Spotify's "Liked Songs" playlist. In YouTube Music's case, the song will land in a dedicated "TikTok Songs" playlist, so you do not have to go hunting for it later. TikTok claims that its "Add to Music App" function has resulted in over 1 billion saves globally since its wider rollout in 2024. On a related note, you probably are already aware of the current divest-or-ban issue TikTok's facing in the US. Howard Lutnick, the US Commerce secretary, recently stated in an interview with Fox News Sunday that President Trump loves the platform and sees it as "a good way to communicate with young people," but the platform's US operations must be handled by an American company, not Chinese.
    • 10 years of using end users as free-guineapigs to test half-baked garbage software...what an achievement 🎉
    • For sure! The first family computer when I was a kid didn't even have a hard drive, just dual 5.25" floppy driver. You would boot from the DOS disk in drive A, run your programs from drive B, and maybe sneak a data disk into drive A and just hope it wouldn't try to access something from the DOS folder while you did that. My dad added a 20 MB (yes, megabyte) hard drive to the computer, it was SUCH a quality-of-life improvement, lol!
    • The Settings app was first introduced in Windows 8 and Microsoft continued to add to the settings app in Windows 10.
  • Recent Achievements

    • Week One Done
      andeyhawk65 earned a badge
      Week One Done
    • First Post
      Jake2530 earned a badge
      First Post
    • Explorer
      Deranox went up a rank
      Explorer
    • Week One Done
      John Volks earned a badge
      Week One Done
    • One Month Later
      enric earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      672
    2. 2
      ATLien_0
      251
    3. 3
      Xenon
      176
    4. 4
      neufuse
      138
    5. 5
      +FloatingFatMan
      102
  • Tell a friend

    Love Neowin? Tell a friend!