#define M_PI 3.14159265358979323846264338327950288 #define M_PI 3.14159265358979323846264338327950288Any time you see a decimal floating-point constant with a nonzero fractional part that doesn't end in '5', you're looking at a bug.
EDIT: As long as this grizzled old Fortran programmer is giving out free advice, I'll add two more items every programmer should know about binary floating-point:
a) Every binary floating-point number can be represented exactly in decimal notation if you use enough digits.
b) Those decimal values are the only ones that can be exactly converted to binary; all of the rest require rounding.
That's just silly. If you're writing some famous mathematical constant, the digits should match that constant, and not the requirements of the machine. (Except for the last one being rounded off.) Suppose we had a floating-point machine that gave us maximum 4 digits of decimal precision. I wouldn't define the PI constant as 3.145. That would just look like a typo to people who have PI memorized to half a dozen digits or more. I'd make it 3.14159 (or more) and let the darn compiler find the nearest approximation on the floating-point axis.
depends on the language. for example here it is in go:
Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // http://oeis.org/A000796So, go might have that value in its source, but it's getting rounded to something that would, if represented in decimal, end in 5.
Having so many digits available means that calculations like Pi/2 or other more intricate evaluations can carry more precision until the result is assigned, making calculations involving constants easier to write without losing precision. It also means that there is no occasion in which the floating-point corner cases like infinities, soft underflows, and NaNs arise in constant expressions.
#define PI1 3.141592653589793115997963468544185161590576171875
double pi1 = PI1;
#define PI2 3.14159265358979323846264338327950288
double pi2 = PI2;
assert(pi1 == pi2);
(edit: or even 3.14159265358979323846)
const PI: f64 = 3.14159265358979323846264338327950288;
?I'll accept without looking it up that the statement is correct syntax in some language (it doesn't look like C++ to me).
You wouldn't use a macro for something like that in Rust.
#define PASS_VERBOSE if (flag_verbose && first_pass) printf
you just might be able to but only with a completely different tool.A macro processor is not of the language; it is above the language.
Then feel free to use the C preprocessor with Rust, it works just as well. :P Just like it does with Python, and Java, and...
#define whatever(param) \
foo: \
bar \
baz
I made a preprocessor some 18 years ago that could be used with Python.Wayback Machine:
https://web.archive.org/web/20000815202258/http://users.foot...
It's path::to::wherever::PI. That's it. Just like any other item. If you want to use only PI in your code, you'd use 'use', like any other name.
Your second example is something better suited to a macro, it's true.
I _think_ what you're getting at here is that you only want text substitution? I think we will have to agree to disagree if that's true :)
Yes, I do like textual substitution. Guilty. This is a common old school low level paradigm. Still, the underlying language and its compiler exist below to enforce the rules on any atrocities I commit with my macros.
The kind of macros Rust implements, on the other hand, are the kind that have to be language-specific, because they deal with the syntax tree of that particular language. They also enable many things that are outright impossible with text substitution, such as hygienic macros.
These two statements are not consistent. Textual substitution is not usable. The pitfalls with it have been well documented for decades, and yet the same problems persist. Why on earth would you want to perpetuate the list of problems caused by such a facility?
Syntactic macros are absolutely superior to textual substitution. You can take issue with Rust's currently limited support for macros, but to propose textual substitution as a viable and more usable alternative is simply absurd.
Those who do use and write them heavily are the ones actually involved in the macros 2.0 effort. There are certainly flaws.