473,383 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

definitions of constants such as pi and e

Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.

Thanks.
Apr 8 '06 #1
27 2244
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.

Thanks.


If you couldn't find any, just google for the value and copy.

Ben
Apr 8 '06 #2
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.

Thanks.


FWIW...

There is a history to trying to solve constants problem in an elegant
way with various solutions having being proposed and rejected on
gmane.comp.lib.boost.devel newsgroup.

The solutions fell into 4 areas IIRC:

solution1: Use a variable e.g

//some math header

extern const double pi;

Note that one shouldnt define it in the header due to the ODR rule so
it would require linking a library in the application or defining in a
source file, which was felt to be inconvenient. The other drawback was
that it cant simultaneously be float, double and long double so it will
never be right for everybody.

solution 2: use a function e.g

inline double pi()
{
return 3.141592653589793238462643383279502884197;
}

This suffers in that you cant control the floating point type and it
seems that people hate the function notation to call it. You could make
it a template of course, but you would have to provide the template
parameter when invoked.

solution 3: Use a smart object

struct pi{
template <typename T>
T pi_function(); // get pi as a T
template <typename T>
operator T() { return pi_function<T>();}
}
This may or may not be how it went. Whatever... The problem this had
was deducing the type to convert to in some situations IIRC.

solution 4: My preferred solution and the one I use, but suffers from
various of the problems above

template <typename T>
struct constant_{

static const T pi;
static const T e;
};

// convenient version for My Favourite float
#ifndef MY_FAVOURITE_FLOAT
#define MY_FAVOURITE_FLOAT double
#endif
struct constant : constant_<MY_FAVOURITE_FLOAT>{};

int main()
{
float val1 = constant_<float>::pi;
double val = constant::pi;
}

Of course this requires a library or source and visible use of
templates in the first invocation at least.
Finally there is an M_PI macro available in some versions of <cmath>
normally via its nested include <math.h>, but that is horrible...

So If anyone has any ideas .... ???

regards
Andy Little

Apr 8 '06 #3
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.


Here are the ones used in D's std.math, good to 80 bits:

const real E = 2.7182818284590452354L;
const real LOG2T = 0x1.a934f0979a3715fcp+1;
const real LOG2E = 0x1.71547652b82fe178p+0;
const real LOG2 = 0x1.34413509f79fef32p-2;
const real LOG10E = 0.43429448190325182765;
const real LN2 = 0x1.62e42fefa39ef358p-1;
const real LN10 = 2.30258509299404568402;
const real PI = 0x1.921fb54442d1846ap+1;
const real PI_2 = 1.57079632679489661923;
const real PI_4 = 0.78539816339744830962;
const real M_1_PI = 0.31830988618379067154;
const real M_2_PI = 0.63661977236758134308;
const real M_2_SQRTPI = 1.12837916709551257390;
const real SQRT2 = 1.41421356237309504880;
const real SQRT1_2 = 0.70710678118654752440;

The hex float notation is C99 compatible. I prefer hex float for
constants, as it ensures accuracy to the last bit without worrying about
decimal conversion roundoff errors.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Apr 8 '06 #4
Walter Bright wrote:
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.


Here are the ones used in D's std.math, good to 80 bits:


[...]

Of course C++ has a selection of floating point types, each with
implementation defined behaviour... a clear **win** [note1] over the
single 'real' type in D

[note1]
meant with a hint of sarcasm...;-)

regards
Andy Little

Apr 8 '06 #5
"Matt" writes:
Does the language have header files containing definitions of numbers such
as pi and e?


No, the standard language does not. One common addition to some compilers
is to put them in <math.h> as macros with names such as M_PI.
Apr 8 '06 #6
benben wrote:
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find
them in Stroustrup.

Thanks.

If you couldn't find any, just google for the value and copy.

Ben


.... ha ha very practical ...
Apr 8 '06 #7
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:0I****************@news01.roc.ny...
benben wrote:
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.

Thanks.

If you couldn't find any, just google for the value and copy.

Ben


... ha ha very practical ...


Ha ha. Look what I found:

http://pi.lacim.uqam.ca/eng/table_en.html

BTW, when you put such constants in your program:

1) Keep 35 significant digits. This guarantees good results for 113-bit
(16-byte) long double, which needs 33+ digits, but avoids the common
compiler bug that causes overflow while the constant is being formed,
which occurs around 38 digits.

2) Always append F for float or L for long double.

3) Don't bother with C99-style hexadecimal, the decimal form is going
to be accurate enough for your needs.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Apr 8 '06 #8
osmium wrote:
"Matt" writes:
Does the language have header files containing definitions of numbers such
as pi and e?


No, the standard language does not. One common addition to some compilers
is to put them in <math.h> as macros with names such as M_PI.


#include <cmath>

const double e = std::exp(1.0);
const double pi = std::atan(1.0) * 4.0;

Apr 8 '06 #9
P.J. Plauger wrote:
Ha ha. Look what I found:

http://pi.lacim.uqam.ca/eng/table_en.html

BTW, when you put such constants in your program:

1) Keep 35 significant digits. This guarantees good results for 113-bit
(16-byte) long double, which needs 33+ digits, but avoids the common
compiler bug that causes overflow while the constant is being formed,
which occurs around 38 digits.

2) Always append F for float or L for long double.

3) Don't bother with C99-style hexadecimal, the decimal form is going
to be accurate enough for your needs.


Since we never know what someone else's needs are, it's best to provide
the constants accurate to the last bit. The reason to use C99 hex floats
is to avoid things like "the common compiler bug" that goof up the
conversion from decimal to float. Another problem with some conversion
algorithms is that some bit patterns cannot be arrived at by
manipulating the decimal value - it may 'skip' over the last bit. With
hex floats you can just set the right bit pattern, without wasting time
twiddling with various decimal values trying to asymptotically approach
it. Even worse, installing a compiler update might slightly change the
compiler's algorithm, meaning you have to start over (if you even notice
it's gone awry). One particularly fun game of brinksmanship is to get
LDBL_MAX without falling over into infinity. It's easy with hex floats:

#define LDBL_MAX 0x1.FFFFFFFFFFFFFFFEp+16383L

It's too bad it's hard to find reliable references to bit patterns for
the constants. The ones I use are from Hart & Cheney's "Computer
Approximations."

Many compilers still inexplicably don't support hex floats, for those
I've seen people (i.e. Stephen Moshier in his most excellent Cephes math
library) forced to resort to reinterpret casting of tuples of longs.

(An awfully common problem is one you referenced above - failing to
append an 'L'. The symptoms can be very subtle. It's one I've fallen
victim to enough times that D will internally will treat constants to
the full 80 bit precision, even if they are typed as doubles. The only
time they get rounded to 64 bits is when they are actually assigned to a
double.)

-Walter Bright
www.digitalmars.com C, C++, D programming languages
Apr 9 '06 #10
red floyd wrote:
osmium wrote:
"Matt" writes:
Does the language have header files containing definitions of numbers
such as pi and e?

No, the standard language does not. One common addition to some
compilers is to put them in <math.h> as macros with names such as M_PI.


#include <cmath>

const double e = std::exp(1.0);
const double pi = std::atan(1.0) * 4.0;


That seems good, assuming the compiler would optimize away the function
calls.

Still I guess you would have a problem if you were to assign to a float.
So you need definitions that use expf and atanf too.
Apr 11 '06 #11
red floyd wrote:
osmium wrote:
"Matt" writes:
Does the language have header files containing definitions of numbers
such as pi and e?

No, the standard language does not. One common addition to some
compilers is to put them in <math.h> as macros with names such as M_PI.


#include <cmath>

const double e = std::exp(1.0);
const double pi = std::atan(1.0) * 4.0;


Please disregard my preceding remark about the compiler optimizing away
the function call.
Apr 11 '06 #12
P.J. Plauger wrote:
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:0I****************@news01.roc.ny...

benben wrote:
Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can't find them
in Stroustrup.

Thanks.
If you couldn't find any, just google for the value and copy.

Ben


... ha ha very practical ...

Ha ha. Look what I found:

http://pi.lacim.uqam.ca/eng/table_en.html

BTW, when you put such constants in your program:

1) Keep 35 significant digits. This guarantees good results for 113-bit
(16-byte) long double, which needs 33+ digits, but avoids the common
compiler bug that causes overflow while the constant is being formed,
which occurs around 38 digits.

2) Always append F for float or L for long double.

3) Don't bother with C99-style hexadecimal, the decimal form is going
to be accurate enough for your needs.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


I wonder whether supplying a literal has any advantages over
initializing a constant to say 4*atan(1) for pi.

Apr 11 '06 #13
Matt wrote:
red floyd wrote:
osmium wrote:
"Matt" writes:

Does the language have header files containing definitions of
numbers such as pi and e?
No, the standard language does not. One common addition to some
compilers is to put them in <math.h> as macros with names such as M_PI.


#include <cmath>

const double e = std::exp(1.0);
const double pi = std::atan(1.0) * 4.0;


That seems good, assuming the compiler would optimize away the function
calls.

Still I guess you would have a problem if you were to assign to a float.
So you need definitions that use expf and atanf too.


Why?

Double converts to float easily.

float f = static_cast<float>(e);
float p = static_cast<float>(pi);
Apr 11 '06 #14
Matt wrote:
I wonder whether supplying a literal has any advantages over
initializing a constant to say 4*atan(1) for pi.


1) It doesn't need code to initialize it at run time.
2) It's probably more accurate.
Apr 11 '06 #15
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:rs****************@news01.roc.ny...
P.J. Plauger wrote:
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:0I****************@news01.roc.ny...

benben wrote:

Matt wrote:
>Does the language have header files containing definitions of numbers
>such as pi and e?
>
>I believe some implementations used to have those, but I can't find
>them in Stroustrup.
>
>Thanks.
If you couldn't find any, just google for the value and copy.

Ben

... ha ha very practical ...

Ha ha. Look what I found:

http://pi.lacim.uqam.ca/eng/table_en.html

BTW, when you put such constants in your program:

1) Keep 35 significant digits. This guarantees good results for 113-bit
(16-byte) long double, which needs 33+ digits, but avoids the common
compiler bug that causes overflow while the constant is being formed,
which occurs around 38 digits.

2) Always append F for float or L for long double.

3) Don't bother with C99-style hexadecimal, the decimal form is going
to be accurate enough for your needs.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


I wonder whether supplying a literal has any advantages over initializing
a constant to say 4*atan(1) for pi.


It's more likely to be correct.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Apr 11 '06 #16
"P.J. Plauger" writes:
I wonder whether supplying a literal has any advantages over initializing
a constant to say 4*atan(1) for pi.


It's more likely to be correct.


But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a few
key strokes.
Apr 11 '06 #17
osmium wrote:
"P.J. Plauger" writes:

I wonder whether supplying a literal has any advantages over initializing
a constant to say 4*atan(1) for pi.


It's more likely to be correct.

But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a few
key strokes.


Which bit of lore?
Apr 20 '06 #18
On Thu, 20 Apr 2006 02:48:00 +0000, Matt wrote:
osmium wrote:
"P.J. Plauger" writes:

I wonder whether supplying a literal has any advantages over
initializing a constant to say 4*atan(1) for pi.

It's more likely to be correct.

But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good


Not unlike using words like "erudition", right? ;^)

Assuming you know what you're doing, basing constants on some identity
rule makes more sense to me since too often typing in a long literal value
will suffer from mistakes of transposed or dropped digits. Of course the
down side is that the formula you use to compute the constant should
have enough precision (and accuracy) to be useful. and let us not forget
that if misused, basing a constant on the calculation of some identity can
lead to inefficient code, such as recomputing the identity over and over
again in a loop (ignoring the forthcoming compiler optimizer arguments
that I know the preceding comment will generate). :^)

It is late. I need beer and tobacco.

-noone of consequence
Apr 20 '06 #19
And this kind of definition of pi ( call funcition atan()) will cause
performance issues, I think there is no good to apply this way otherwise
defining pi as 3.14159 directly

--

Best Regards

Liang Zhang

Developer
SAP Labs China

T: +86 21 61006699-7775
F: +86 21 65984520
Email li*********@sap.com

SAP LABS CHINA : INNOVATE THE FUTURE
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:xz****************@news02.roc.ny...
osmium wrote:
"P.J. Plauger" writes:

I wonder whether supplying a literal has any advantages over
initializing a constant to say 4*atan(1) for pi.

It's more likely to be correct.

But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a
few key strokes.


Which bit of lore?

Apr 21 '06 #20
"Matt" writes:
osmium wrote:
"P.J. Plauger" writes:

I wonder whether supplying a literal has any advantages over
initializing a constant to say 4*atan(1) for pi.

It's more likely to be correct.

But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a
few key strokes.


Which bit of lore?


I can't believe this is a serious question, but anyway. One definition of
lore is "Knowledge acquired through education or experience."

The average person does not know that pi = 4*atan(1) so that makes it lore.

Some more lore:

o How to swap a and b without using a temporary variable
o The problem is on line 42
o The Death Station 9000 will vomit
o Big endian is different than little endian
Apr 21 '06 #21
osmium wrote:
"Matt" writes:

osmium wrote:
"P.J. Plauger" writes:

>I wonder whether supplying a literal has any advantages over
>initializing a constant to say 4*atan(1) for pi.

It's more likely to be correct.
But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a
few key strokes.


Which bit of lore?

I can't believe this is a serious question, but anyway. One definition of
lore is "Knowledge acquired through education or experience."

The average person does not know that pi = 4*atan(1) so that makes it lore.


The "It" in Mr. Plauger's answer to my question referred to the literal
value, not (as you seemed to believe) to the atan expression. That is a
partial explanation of your disbelief.
Apr 22 '06 #22
osmium wrote:
"P.J. Plauger" writes:

I wonder whether supplying a literal has any advantages over initializing
a constant to say 4*atan(1) for pi.


It's more likely to be correct.

But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a few
key strokes.


Ah yes, I remember those days well, the group gathered 'round the
campfire listening to the elders ... one old sage telling us how he got
out of a tight spot once by integrating the gamma function ... and the
tall thin old trig teacher admonishing us to always remember that 4
times the arctan of 1 is pi ... But it was only years after my
initiation that I was informed by another graybeard that that holds for
_all_ values of 1 ...
Apr 22 '06 #23
osmium wrote:
"P.J. Plauger" writes:
I wonder whether supplying a literal has any advantages over initializing
a constant to say 4*atan(1) for pi.

It's more likely to be correct.


But the more likely reason it is used is someone simply showing off his
erudition. Anyone who knows this bit of lore also has a perfectly good
value for pi available to him somewhere in his archives or by making a few
key strokes.


My roommate in college once wrote a program to generate Pi to 10,000
places. To test it, he'd run it to generate 2 or 3 hundred places. I
asked him how he knew it was producing the right values. He said he knew
just by looking at it, because in high school he'd memorized Pi to 800
places.
Apr 24 '06 #24
> The average person does not know that pi = 4*atan(1) so that makes it lore.

The average person never went to high school but is programming in C++
sounds super feasible. I always thought that trigonometry is
mathematics and very commonly known. Doh!

Apr 25 '06 #25
persenaama posted:
The average person does not know that pi = 4*atan(1) so that makes it
lore.


The average person never went to high school but is programming in C++
sounds super feasible. I always thought that trigonometry is
mathematics and very commonly known. Doh!

But it's a certain kind of person who has a true interest in trigonometry.
For people like myself, I learn it in school/college, and three months
later it's forgotten.
-Tomás
Apr 25 '06 #26
"persenaama" writes:
The average person does not know that pi = 4*atan(1) so that makes it
lore.


The average person never went to high school but is programming in C++
sounds super feasible. I always thought that trigonometry is
mathematics and very commonly known. Doh!


The gal who taught me trig wouldn't understand what that meant. But she
knew what 4 tan^-1(1) and what 4 arc tan(1) were. You probably went to a
better school than I did, we couldn't all be that lucky. <Sigh>
Apr 25 '06 #27
"osmium" wrote:
"persenaama" writes:
The average person does not know that pi = 4*atan(1) so that makes it
lore.


The average person never went to high school but is programming in C++
sounds super feasible. I always thought that trigonometry is
mathematics and very commonly known. Doh!


The gal who taught me trig wouldn't understand what that meant. But she
knew what 4 tan^-1(1) and what 4 arc tan(1) were. You probably went to a
better school than I did, we couldn't all be that lucky. <Sigh>


On further thought, I have no reason to believe that my high school teacher
had any notion of what a radian was (or is). You can teach trigonometry
perfectly well by measuring angles in degrees. She would probably have
looked in a table in a book and seen that a 45 degree angle corresponds to a
tangent of 1. So 4 x arc tan(1) = 4 x 45 and pi should, of course, be set
to 180.
Apr 25 '06 #28

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Palumbo | last post by:
Hi, I have a question about defining long string constants in class defintions. For an example, let's say I have some long SQL queries that I'd like to store in a class. Furthermore, I don't...
9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
0
by: vholt | last post by:
I have small working sample in C# of using the NetUserGetInfo external call to netapi32.dll. I would like to define some constants to use for comparison to return codes, but I can not find the...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
0
by: Steve - DND | last post by:
I have a web project which references some assemblies from other projects I have worked on in different solutions, and am running into some quirky behavior. My web project assembly is...
8
by: Thelma Lubkin | last post by:
I have finally nagged more work out of the non-profit organization that I'm trying to do volunteer work for. The following code begins a tiny form that is part of the project that I'll be...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.