0

So I want to work out eqations for things such as the circumfrance of a circle that use things such as pi/π. Looked around and worked out that I need to use

 #define _USE_MATH_DEFINES  
 #include "math.h"
 
and

 M_2_PI //presumably

But I'm having issues figuring out how to piece this together since the π doesn't exist and you can't do a simple line such as on a calculator. If my radius input is "r" and I try to use something like x = M_2_PI * r; I'll get close results, but close isn't enough in maths.

Are there some instructions or guidelines inside VS to work this better?

Arty
  • 13
  • 5
  • Would you please elaborate on what you mean by "*I'll get close results, but close isn't enough in maths*"? – Aziz Jul 30 '20 at 00:51
  • 1
    Thanks. The suggestion had the right thing of "2*M_PI" in the comments while I was confused and using the wrong one of "M_2_PI" to work out my stuff. – Arty Jul 30 '20 at 00:57

1 Answers1

1

If my radius input is r and I try to use something like x = M_2_PI * r; I'll get close results, but close isn't enough in maths.

Arithmetic operations on intrinsic number types in pretty-much every non-mathematics programming language will always give you only approximate results - because that's how integer-types and IEE-754 floating point numbers work. There's no (easy) way around it.

(For example, while you can use a Rational-type (such as boost::rational]2) to for exact representations of fractional values this approach won't work with irrational numbers like Pi).

If you want an exact answer, then don't use C - instead use a system designed for high-level mathematics which knows how to represent exact values and operate on mathematical expressions rather than approximate numerical values - these systems are known as a CAS (Computer Algebra System) like Wolfram Mathematica. Note that you shouldn't confuse CAS systems with non-symbolic Numerics Systems such as Matlab and Mathcad (though to an extent almost every mathematics system is capable of both symbolic and numeric operations).

(I credit Mathematica for getting me through my A2 Pure and FP courses). TI also sell a line of handheld CAS calculators (I have an nSpire CAS), which are predictably banned for use in most high-level and undergraduate-level exams).

Dai
  • 141,631
  • 28
  • 261
  • 374