Hi,
I'm doing some maths and I've just found that the standard library
does not provide a standard PI value. In older post I see everyone says
you must define it by yourself, but as some of the standard functions
are in fact supposed to return that value, I feel there must be a better
way. For example, here's what a google search returned:
float PI = std::atan(1.0f) * 4.0f;
Anything more accurate?
Thanks 16 37929
giaro wrote:
Hi,
I'm doing some maths and I've just found that the standard library
does not provide a standard PI value. In older post I see everyone
says you must define it by yourself, but as some of the standard
functions are in fact supposed to return that value, I feel there
must be a better way. For example, here's what a google search
returned:
float PI = std::atan(1.0f) * 4.0f;
Anything more accurate?
This should be accurate enough.
You can define one yourself (3.1415926f for float should be enough)
and then compare this to the result you get using atan.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
giaro wrote:
float PI = std::atan(1.0f) * 4.0f;
Anything more accurate?
std::atan(1.0f) should return pi/4 to within 0.5 ulp (though I don't think
the standard guarantees this). Multiplying by 4.0 will still give an answer
accurate to 0.5 ulp if the floating-point base is 2 (or 4). So I would think
that more accuracy is impossible. Perhaps pi = std::atan2(0.0f,-1.0f) would
be slightly safer since it doesn't depend on the base.
-- Ben
giaro <no****@here.inwrote:
float PI = std::atan(1.0f) * 4.0f;
Anything more accurate?
double pi = 4 * std::atan(1);
Any inaccuracy here is likely due to the limited precision of float or double
rather than this method of computation. I checked by getting 22 digits or so
from http://www.piday.org/million.php and when assigned to double the result
matches exactly to pi as computed above. (gcc 4.2.1, glibc 2.6.1)
Clemens
giaro schrieb:
I'm doing some maths and I've just found that the standard library
does not provide a standard PI value.
Isn't "M_PI" a defined symbol? Maybe I mix it up with something else?
Greetings,
Johannes
--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
On 2007-12-01 12:51, giaro wrote:
Hi,
I'm doing some maths and I've just found that the standard library
does not provide a standard PI value. In older post I see everyone says
you must define it by yourself, but as some of the standard functions
are in fact supposed to return that value, I feel there must be a better
way. For example, here's what a google search returned:
float PI = std::atan(1.0f) * 4.0f;
Anything more accurate?
If your implementation is POSIX compliant (most are) you should have
M_PI defined which should be as accurate as double permits.
--
Erik Wikström
"Johannes Bauer" writes:
giaro schrieb:
> I'm doing some maths and I've just found that the standard library does not provide a standard PI value.
Isn't "M_PI" a defined symbol? Maybe I mix it up with something else?
M_PI is an add on offered by many compilers, including Borland. Thus it is
not standard. ISTM that the authors of the standard could have found a
better way/wording to handle extensions, thus avoiding this recurring
misunderstanding.
osmium schrieb:
M_PI is an add on offered by many compilers, including Borland. Thus it is
not standard. ISTM that the authors of the standard could have found a
better way/wording to handle extensions, thus avoiding this recurring
misunderstanding.
That's a shame - is this resolved in C++0x? Pi is a constant which is
*very* common, one could also include Planck's constant and the
Avogadro-number IMHO (as it doesn't make any difference if not used).
Greetings,
Johannes
--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
"Johannes Bauer" writes:
osmium schrieb:
>M_PI is an add on offered by many compilers, including Borland. Thus it is not standard. ISTM that the authors of the standard could have found a better way/wording to handle extensions, thus avoiding this recurring misunderstanding.
That's a shame - is this resolved in C++0x? Pi is a constant which is
*very* common, one could also include Planck's constant and the
Avogadro-number IMHO (as it doesn't make any difference if not used).
I guess there is some clever sarcasm in there but I am unable to disentangle
it to find your point so I can't respond to it.
*My* point was that there is no reasonable way to determine whether an
identifier is valid and usable. And the only way I know of to see if a
*program's* source code is valid is to compile it on some sort of
certified - sort of - compiler. Perhaps Comeau.
The compiler's vendors have done cherry picking on the list of valid id's
and appropriated some of them for their own use, including M_PI. They are
then permitted to legally add
their declaration to a standard library, cmath. If I accidentally use M_PI
as an identifier I may see some very mysterious error messages. OTOH if I
release what I think is a standard bit of source code I
have to go through a private hell to confirm that it really can, in fact,
be compiled at will by someone else.
Whether constants should be included in a programming language or not is an
entirely separate issue - my current argument is the method permitted, not
the desired end result.
On 2007-12-01 15:57:02 -0500, "osmium" <r1********@comcast.netsaid:
>
The compiler's vendors have done cherry picking on the list of valid id's
and appropriated some of them for their own use, including M_PI. They are
then permitted to legally add
their declaration to a standard library, cmath.
Legally, yes: there's no law prohibiting it. But adding identifiers
that aren't specified by the language definition (other than names
reserved to the implementor, which M_PI is not) makes the
implementation non-conforming.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
On Dec 1, 7:41 pm, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:
osmium schrieb:
M_PI is an add on offered by many compilers, including
Borland. Thus it is not standard. ISTM that the authors of
the standard could have found a better way/wording to
handle extensions, thus avoiding this recurring
misunderstanding.
That's a shame - is this resolved in C++0x? Pi is a constant
which is *very* common, one could also include Planck's
constant and the Avogadro-number IMHO (as it doesn't make any
difference if not used).
Not that I know of. It's an interesting point. Having
something along the lines of numeric_limits, but for a number of
notable constants (pi and e are, of course, essential, but there
are doubtlessly a number of others which would make sense)
actually sounds like a good idea, but I'm not aware of anyone
ever having made such a proposal. (One immediate question:
should only dimensionless constants be considered? Or should
one also support things like the gravitational constant, and if
so, only in SI, are also in other common systems.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On 2007-12-02 09:05:42 -0500, "Alf P. Steinbach" <al***@start.nosaid:
* James Kanze:
>On Dec 1, 7:41 pm, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:
>>osmium schrieb:
>>>M_PI is an add on offered by many compilers, including Borland. Thus it is not standard. ISTM that the authors of the standard could have found a better way/wording to handle extensions, thus avoiding this recurring misunderstanding.
>>That's a shame - is this resolved in C++0x? Pi is a constant which is *very* common, one could also include Planck's constant and the Avogadro-number IMHO (as it doesn't make any difference if not used).
Not that I know of. It's an interesting point. Having something along the lines of numeric_limits, but for a number of notable constants (pi and e are, of course, essential, but there are doubtlessly a number of others which would make sense) actually sounds like a good idea, but I'm not aware of anyone ever having made such a proposal.
It's been discussed in clc++, clc++m and I think also csc++ several
times. I've asked about it a number of times.
As always: if it matters to you, write a proposal. It's far too late
for C++0x, though.
>
> (One immediate question: should only dimensionless constants be considered? Or should one also support things like the gravitational constant, and if so, only in SI, are also in other common systems.)
And that's how such practical ideas end. Instead of thinking
standardization of existing practice (should be easy, M_PI and possibly
M_E, whatever), the measure must also save the world.
Speculating about natural extensions is a critical part of design.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
On Dec 1, 10:32 am, "osmium" <r124c4u...@comcast.netwrote:
"Johannes Bauer" writes:
giaro schrieb:
I'm doing some maths and I've just found that the standard library
does not provide a standard PI value.
Isn't "M_PI" a defined symbol? Maybe I mix it up with something else?
M_PI is an add on offered by many compilers, including Borland. Thus it is
not standard. ISTM that the authors of the standard could have found a
better way/wording to handle extensions, thus avoiding this recurring
misunderstanding.
M_PI, M_SQRT2, M_LN2 and the other, related macros that are found in
math.h - are defined by the POSIX Standard - and have been for years.
See: http://www.opengroup.org/onlinepubs/...fs/math.h.html
So there is certainly no reason not to use M_PI - especially since the
POSIX Standard guarantees that M_PI is the most accurate value for pi
that a double can represent.
Greg
On Dec 3, 1:35 pm, Greg Herlihy <gre...@mac.comwrote:
On Dec 1, 10:32 am, "osmium" <r124c4u...@comcast.netwrote:
"Johannes Bauer" writes:
giaro schrieb:
> I'm doing some maths and I've just found that the standard library
>does not provide a standard PI value.
Isn't "M_PI" a defined symbol? Maybe I mix it up with something else?
M_PI is an add on offered by many compilers, including Borland. Thus it is
not standard. ISTM that the authors of the standard could have found a
better way/wording to handle extensions, thus avoiding this recurring
misunderstanding.
M_PI, M_SQRT2, M_LN2 and the other, related macros that are found in
math.h - are defined by the POSIX Standard - and have been for years.
See:
http://www.opengroup.org/onlinepubs/...fs/math.h.html
So there is certainly no reason not to use M_PI - especially since the
POSIX Standard guarantees that M_PI is the most accurate value for pi
that a double can represent.
Greg
Are they in ansi C stardard? As far as I am concerned,the posix don't
care about the math related things.
On Dec 3, 6:35 am, Greg Herlihy <gre...@mac.comwrote:
[...]
So there is certainly no reason not to use M_PI - especially since the
POSIX Standard guarantees that M_PI is the most accurate value for pi
that a double can represent.
Which means that it's less than what is wanted if I'm using long
double. IIRC, Intel processors have a built-in pi constant.
Using this, instead of any external constant, should result in
greater precision. I'm not sure how Posix defined it, but some
sort of compiler magic triggering use of this built in constant
should be allowed, i.e.:
#define M_PI __builtin_pi
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Sat, 1 Dec 2007 16:48:55 +0000 (UTC), Clemens Buchacher wrote:
giaro <no****@here.inwrote:
>float PI = std::atan(1.0f) * 4.0f;
>Anything more accurate?
double pi = 4 * std::atan(1);
Or double pi = std::atan2(0,-1);
Because for constants, 0, 1 and -1 feel less magical than 4 does :)
--
Joel Yliluoma - http://iki.fi/bisqwit/
On Dec 2, 5:53 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-02 09:05:42 -0500, "Alf P. Steinbach" <al...@start.nosaid:
* James Kanze:
[...]
(One immediate question:
should only dimensionless constants be considered? Or should
one also support things like the gravitational constant, and if
so, only in SI, are also in other common systems.)
And that's how such practical ideas end. Instead of thinking
standardization of existing practice (should be easy, M_PI and possibly
M_E, whatever), the measure must also save the world.
Speculating about natural extensions is a critical part of design.
In this case, "speculating" is very much the correct word. I
wasn't making a proposal, just speculating as to what a C++
proposal might look like. Fundamentally, I more or less agree
with Alf; just make the Posix extension part of standard C (and
because it's in <math.h>, it's really a question for C, and not
C++), for starters. About the only point I'd insist on is that
the macros could expand to things like __builtin_pi, so that the
compiler could actually use the value built into the math
processor, if it exists.
Of course, since I don't do much numeric programming anyway,
it's no big thing for me, and if the numericists don't care (and
apparently they don't since they've never presented a proposal),
that's their problem, not mine.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Edwin Young |
last post by:
Hi,
I think there might (emphasize might) be a minor bug in Python's cmath library.
Basically, Python says cmath.asin(0) = 0-0j (+0 real part, -0 imaginary part).
I think it should be 0+0j...
|
by: Robert Carlson |
last post by:
I am trying to extract the value from a selected item in a
Select/Option, parse it server side using asp and then populate a text
box on the same page with the value that was parsed. simple example...
|
by: Cognizance |
last post by:
Hi gang,
I'm an ASP developer by trade, but I've had to create client side
scripts with JavaScript many times in the past. Simple things, like
validating form elements and such.
Now I've been...
|
by: rowyerboat |
last post by:
I am trying to compare a value from a datarow with
another value.
In debug mode, the values either side of the comparison
statement are exactly the same, but when the code runs it
skips over...
|
by: nephish |
last post by:
hey there,
i have looked at the string module and re.
i was looking for advice on what would be the best way to pull a value
out of a small string.
for example, i have a string
$.+.09 JAR...
|
by: Randy |
last post by:
Is there any way to do this? I've tried tellg() followed by seekg(),
inserting the stream buffer to an ostringstream (ala os << is.rdbuf()),
read(), and having no luck.
The problem is, all of...
|
by: Mark Dickinson |
last post by:
I was a little surprised by the following behaviour:
Python 2.5 (r25, Oct 30 2006, 20:50:32)
on darwin
Type "help", "copyright", "credits" or "license" for more information.
.... def...
|
by: mlevit |
last post by:
Hi,
I need to obtain the value of the selected item from a combo box.
BEGIN
IF :LOGIN_BLOCK.USER_TYPE = 'Student' THEN
:GLOBAL.student_id := :LOGIN_BLOCK.ID;...
|
by: KSor |
last post by:
I have a combobox I want to extract the "selected value" BUT
When I ask in the Immidiate window I get this - an object-valuie I think :
? comboBox_Country.SelectedItem
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |