Topic: A couple of classes for common mathematical
Author: Herb Sutter <hsutter@microsoft.com>
Date: Wed, 2 Jan 2013 01:54:43 +0000
Raw View
--_000_d9cca33344254ecdb939761c1635c025BLUPR03MB084namprd03pro_
Content-Type: text/plain; charset=ISO-8859-1
I can see the image, but text is much easier to read and will be required for a proposal - never mind that clearly you have the text right there on your screen and can just cut and paste. :)
Could you please resend your suggestion with the sample implementation as a part of your draft proposal text? Thanks,
Herb
From: VinceRev [mailto:vince.rev@gmail.com]
Sent: Tuesday, January 1, 2013 5:34 PM
To: std-proposals@isocpp.org
Subject: [std-proposals] A couple of classes for common mathematical constants ?
Hello.
I find this very strange (maybe there are some good reasons that I don't know) not to have common mathematical constants like pi in the standard and to be forced to use syntaxes like 4*arctan(1) to get the value of pi.
So I have written a couple of very simple classes to solve that problem, presented in the joined image.
1) numerical_constant, based on the same syntax as std::integral_constant
2) math_constants, based on the same syntax as numeric_limits
Do you think that could be standardized ?
Vincent
--
--
--_000_d9cca33344254ecdb939761c1635c025BLUPR03MB084namprd03pro_
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
..MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D">I can see the image, but =
text is much easier to read and will be required for a proposal – nev=
er mind that clearly you have the text right there on your screen
and can just cut and paste. :)<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D"><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D">Could you please resend y=
our suggestion with the sample implementation as a part of your draft propo=
sal text? Thanks,<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D"><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D">Herb<o:p></o:p></span></p=
>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D"><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1F497D"><o:p> </o:p></span><=
/p>
<div style=3D"border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in =
4.0pt">
<div>
<div style=3D"border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in =
0in 0in">
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:"=
;Tahoma","sans-serif"">From:</span></b><span style=3D"font-s=
ize:10.0pt;font-family:"Tahoma","sans-serif""> VinceRev=
[mailto:vince.rev@gmail.com]
<br>
<b>Sent:</b> Tuesday, January 1, 2013 5:34 PM<br>
<b>To:</b> std-proposals@isocpp.org<br>
<b>Subject:</b> [std-proposals] A couple of classes for common mathematical=
constants ?<o:p></o:p></span></p>
</div>
</div>
<p class=3D"MsoNormal"><o:p> </o:p></p>
<p class=3D"MsoNormal">Hello.<br>
<br>
I find this very strange (maybe there are some good reasons that I don't kn=
ow) not to have common mathematical constants like pi in the standard and t=
o be forced to use syntaxes like 4*arctan(1) to get the value of pi.<br>
<br>
So I have written a couple of very simple classes to solve that problem, pr=
esented in the joined image.<br>
1) numerical_constant, based on the same syntax as std::integral_constant<b=
r>
2) math_constants, based on the same syntax as numeric_limits<br>
<br>
Do you think that could be standardized ?<br>
<br>
Vincent<br>
<o:p></o:p></p>
<p class=3D"MsoNormal">-- <br>
<br>
<br>
<o:p></o:p></p>
</div>
</div>
</body>
</html>
<p></p>
-- <br />
<br />
<br />
<br />
--_000_d9cca33344254ecdb939761c1635c025BLUPR03MB084namprd03pro_--
.
Author: Vincent R <vince.rev@gmail.com>
Date: Wed, 2 Jan 2013 03:05:33 +0100
Raw View
--14dae93b5c727488b304d244ae5c
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Yes, here it is (if you think that this design could be a good idea, I can
write a proposal as a latex document of course). (+ I realized that 2 const
were omitted in the image)
------------------------------------------------------------------------
#include <iostream>
// Numerical constant class, based on the model of std::integral_constant
template<typename T>
struct numerical_constant
{
typedef T value_type;
typedef numerical_constant<T> type;
constexpr numerical_constant(const value_type val): value{val} {}
constexpr value_type operator()() const {return value;}
constexpr operator value_type() const {return value;}
template<class U> constexpr U cast() const {return value;} // To be
discussed
const value_type value;
};
// Common mathematical constants, based on the model of std::numeric_limits
template<typename T>
struct math_constants
{
static constexpr numerical_constant<T> pi =3D
numerical_constant<T>(3.141592653589793238462643383279502884197169399375105=
8209749445L);
// General: pi
static constexpr numerical_constant<T> e =3D
numerical_constant<T>(2.718281828459045235360287471352662497757247093699959=
5749669676L);
// General: Napier's constant
static constexpr numerical_constant<T> euler =3D
numerical_constant<T>(0.577215664901532860606512090082402431042159335939923=
5988057672L);
// General: Euler=E2=80=93Mascheroni constant
static constexpr numerical_constant<T> phi =3D
numerical_constant<T>(1.618033988749894848204586834365638117720309179805762=
8621354486L);
// General: golden ratio
static constexpr numerical_constant<T> pythagoras =3D
numerical_constant<T>(1.414213562373095048801688724209698078569671875376948=
0731766797L);
// General: Pythagoras' constant, square root of 2
static constexpr numerical_constant<T> theodorus =3D
numerical_constant<T>(1.732050807568877293527446341505872366942805253810380=
6280558069L);
// General: Theodorus' constant, square root of 3
static constexpr numerical_constant<T> degree =3D
numerical_constant<T>(0.017453292519943295769236907684886127134428718885417=
2545609719L);
// General: value of a degree in radians
static constexpr numerical_constant<T> glaisher =3D
numerical_constant<T>(1.282427129100622636875342568869791727767688927325001=
1920637400L);
// Number theory: Glaisher=E2=80=90Kinkelin constant
static constexpr numerical_constant<T> khinchin =3D
numerical_constant<T>(2.685452001065306445309714835481795693820382293994462=
9530511523L);
// Number theory: Khinchin's constant
static constexpr numerical_constant<T> soldner =3D
numerical_constant<T>(1.451369234883381050283968485892027449493032283648015=
8630930045L);
// Number theory: Ramanujan=E2=80=93Soldner constant
static constexpr numerical_constant<T> mertens =3D
numerical_constant<T>(0.261497212847642783755426838608695859051566648261199=
2061920642L);
// Number theory: Meissel=E2=80=93Mertens constant
static constexpr numerical_constant<T> plastic =3D
numerical_constant<T>(1.324717957244746025960908854478097340734404056901733=
3645340150L);
// Number theory: plastic number
static constexpr numerical_constant<T> catalan =3D
numerical_constant<T>(0.915965594177219015054603514932384110774149374281672=
1342664981L);
// Combinatorics: Catalan's constant
static constexpr numerical_constant<T> feigenbaum_delta =3D
numerical_constant<T>(4.669201609102990671853203820466201617258185577475768=
6327456513L);
// Chaos theory: first Feigenbaum constant
static constexpr numerical_constant<T> feigenbaum_alpha =3D
numerical_constant<T>(2.502907875095892822283902873218215786381271376727149=
9773361920L);
// Chaos theory: second Feigenbaum constant
static constexpr numerical_constant<T> omega =3D
numerical_constant<T>(0.567143290409783872999968662210355549753815787186512=
5081351310L);
// Mathematical analysis: Omega constant
};
// Examples
int main(int argc, char* argv[])
{
// Display some constants
std::cout<<"pi =3D "<<math_constants<double>::pi()<<"\n";
std::cout<<"e =3D "<<math_constants<double>::e()<<"\n";
std::cout<<"euler =3D "<<math_constants<double>::euler()<<"\n";
std::cout<<std::endl;
// Create a new constant
numerical_constant<double> hbar(1.054571726E-34);
std::cout<<"hbar [double] =3D "<<hbar<<"\n";
std::cout<<"hbar [double] =3D "<<hbar()<<"\n";
std::cout<<"hbar [float] =3D "<<hbar.cast<float>()<<"\n";
std::cout<<std::endl;
return 0;
}
------------------------------------------------------------------------
2013/1/2 Herb Sutter <hsutter@microsoft.com>
> I can see the image, but text is much easier to read and will be
> required for a proposal =E2=80=93 never mind that clearly you have the te=
xt right
> there on your screen and can just cut and paste. :)****
>
> ** **
>
> Could you please resend your suggestion with the sample implementation as
> a part of your draft proposal text? Thanks,****
>
> ** **
>
> Herb****
>
> ** **
>
> ** **
>
> *From:* VinceRev [mailto:vince.rev@gmail.com]
> *Sent:* Tuesday, January 1, 2013 5:34 PM
> *To:* std-proposals@isocpp.org
> *Subject:* [std-proposals] A couple of classes for common mathematical
> constants ?****
>
> ** **
>
> Hello.
>
> I find this very strange (maybe there are some good reasons that I don't
> know) not to have common mathematical constants like pi in the standard a=
nd
> to be forced to use syntaxes like 4*arctan(1) to get the value of pi.
>
> So I have written a couple of very simple classes to solve that problem,
> presented in the joined image.
> 1) numerical_constant, based on the same syntax as std::integral_constant
> 2) math_constants, based on the same syntax as numeric_limits
>
> Do you think that could be standardized ?
>
> Vincent
> ****
>
> --
>
>
> ****
>
> --
>
>
>
>
--=20
Vincent Reverdy*
Phd Student @ Laboratory Universe and Theories**
Cosmology and General Relativity Group
Observatory of Paris-Meudon, France
***
--=20
--14dae93b5c727488b304d244ae5c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Yes, here it is (if you think that this design could be a =
good idea, I can write a proposal as a latex document of course). (+ I real=
ized that 2 const were omitted in the image)<br><span style=3D"font-family:=
courier new,monospace">----------------------------------------------------=
--------------------<br>
#include <iostream><br><br>// Numerical constant class, based on the =
model of std::integral_constant<br>template<typename T><br>struct num=
erical_constant<br>{<br>=C2=A0=C2=A0=C2=A0 typedef T value_type;<br>=C2=A0=
=C2=A0=C2=A0 typedef numerical_constant<T> type;<br>
=C2=A0=C2=A0=C2=A0 constexpr numerical_constant(const value_type val): valu=
e{val} {}<br>=C2=A0=C2=A0=C2=A0 constexpr value_type operator()() const {re=
turn value;}<br>=C2=A0=C2=A0=C2=A0 constexpr operator value_type() const {r=
eturn value;}<br>=C2=A0=C2=A0=C2=A0 template<class U> constexpr U cas=
t() const {return value;} // To be discussed<br>
=C2=A0=C2=A0=C2=A0 const value_type value;<br>};<br><br>// Common mathemati=
cal constants, based on the model of std::numeric_limits<br>template<typ=
ename T><br>struct math_constants<br>{<br>=C2=A0=C2=A0=C2=A0 static cons=
texpr numerical_constant<T> pi=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T&g=
t;(3.1415926535897932384626433832795028841971693993751058209749445L); // Ge=
neral: pi<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> e=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 =3D numerical_constant<T>(2.7182818284590452353602874713526624=
977572470936999595749669676L); // General: Napier's constant<br>=C2=A0=
=C2=A0=C2=A0 static constexpr numerical_constant<T> euler=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_consta=
nt<T>(0.5772156649015328606065120900824024310421593359399235988057672=
L); // General: Euler=E2=80=93Mascheroni constant<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> phi=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =
=3D numerical_constant<T>(1.61803398874989484820458683436563811772030=
91798057628621354486L); // General: golden ratio<br>=C2=A0=C2=A0=C2=A0 stat=
ic constexpr numerical_constant<T> pythagoras=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 =3D numerical_constant<T>(1.4142135623730950488016887242=
096980785696718753769480731766797L); // General: Pythagoras' constant, =
square root of 2<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> theodorus=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T>(=
1.7320508075688772935274463415058723669428052538103806280558069L); // Gener=
al: Theodorus' constant, square root of 3<br>=C2=A0=C2=A0=C2=A0 static =
constexpr numerical_constant<T> degree=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T>(0.0174532925=
199432957692369076848861271344287188854172545609719L); // General: value of=
a degree in radians<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> glaisher=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T&g=
t;(1.2824271291006226368753425688697917277676889273250011920637400L); // Nu=
mber theory: Glaisher=E2=80=90Kinkelin constant<br>=C2=A0=C2=A0=C2=A0 stati=
c constexpr numerical_constant<T> khinchin=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T>(2.6854520010653064453=
097148354817956938203822939944629530511523L); // Number theory: Khinchin=
9;s constant<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> soldner=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant&=
lt;T>(1.4513692348833810502839684858920274494930322836480158630930045L);=
// Number theory: Ramanujan=E2=80=93Soldner constant<br>=C2=A0=C2=A0=C2=A0=
static constexpr numerical_constant<T> mertens=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant<T>(0.2614972=
128476427837554268386086958590515666482611992061920642L); // Number theory:=
Meissel=E2=80=93Mertens constant<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> plastic=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_constant&=
lt;T>(1.3247179572447460259609088544780973407344040569017333645340150L);=
// Number theory: plastic number<br>=C2=A0=C2=A0=C2=A0 static constexpr nu=
merical_constant<T> catalan=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 =3D numerical_constant<T>(0.9159655941772190150546035149=
323841107741493742816721342664981L); // Combinatorics: Catalan's consta=
nt<br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> feigenbaum_=
delta =3D numerical_constant<T>(4.66920160910299067185320382046620161=
72581855774757686327456513L); // Chaos theory: first Feigenbaum constant<br=
>=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> feigenbaum=
_alpha =3D numerical_constant<T>(2.5029078750958928222839028732182157=
863812713767271499773361920L); // Chaos theory: second Feigenbaum constant<=
br>
=C2=A0=C2=A0=C2=A0 static constexpr numerical_constant<T> omega=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D numerical_=
constant<T>(0.5671432904097838729999686622103555497538157871865125081=
351310L); // Mathematical analysis: Omega constant<br>};<br><br>// Examples=
<br>
int main(int argc, char* argv[])<br>{<br>=C2=A0=C2=A0=C2=A0 // Display some=
constants<br>=C2=A0=C2=A0=C2=A0 std::cout<<"pi=C2=A0=C2=A0=C2=
=A0 =3D "<<math_constants<double>::pi()<<"\n&qu=
ot;;<br>=C2=A0=C2=A0=C2=A0 std::cout<<"e=C2=A0=C2=A0=C2=A0=C2=A0=
=3D "<<math_constants<double>::e()<<"\n";=
<br>
=C2=A0=C2=A0=C2=A0 std::cout<<"euler =3D "<<math_cons=
tants<double>::euler()<<"\n";<br>=C2=A0=C2=A0=C2=A0 s=
td::cout<<std::endl;<br>=C2=A0=C2=A0=C2=A0 <br>=C2=A0=C2=A0=C2=A0 // =
Create a new constant<br>=C2=A0=C2=A0=C2=A0 numerical_constant<double>=
; hbar(1.054571726E-34);<br>
=C2=A0=C2=A0=C2=A0 std::cout<<"hbar [double] =3D "<<h=
bar<<"\n";<br>=C2=A0=C2=A0=C2=A0 std::cout<<"hba=
r [double] =3D "<<hbar()<<"\n";<br>=C2=A0=C2=A0=
=C2=A0 std::cout<<"hbar [float]=C2=A0 =3D "<<hbar.cas=
t<float>()<<"\n";<br>
=C2=A0=C2=A0=C2=A0 std::cout<<std::endl;<br>=C2=A0=C2=A0=C2=A0 return=
0;<br>}<br>---------------------------------------------------------------=
---------</span><br></div><div class=3D"gmail_extra"><br><br><div class=3D"=
gmail_quote">2013/1/2 Herb Sutter <span dir=3D"ltr"><<a href=3D"mailto:h=
sutter@microsoft.com" target=3D"_blank">hsutter@microsoft.com</a>></span=
><br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
<div link=3D"blue" vlink=3D"purple" lang=3D"EN-US">
<div>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d">I can see the image, but =
text is much easier to read and will be required for a proposal =E2=80=93 n=
ever mind that clearly you have the text right there on your screen
and can just cut and paste. :)<u></u><u></u></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d"><u></u>=C2=A0<u></u></spa=
n></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d">Could you please resend y=
our suggestion with the sample implementation as a part of your draft propo=
sal text? Thanks,<u></u><u></u></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d"><u></u>=C2=A0<u></u></spa=
n></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d">Herb<u></u><u></u></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d"><u></u>=C2=A0<u></u></spa=
n></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:"Ca=
libri","sans-serif";color:#1f497d"><u></u>=C2=A0<u></u></spa=
n></p>
<div style=3D"border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in =
4.0pt">
<div>
<div style=3D"border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in =
0in 0in">
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:"=
;Tahoma","sans-serif"">From:</span></b><span style=3D"font-s=
ize:10.0pt;font-family:"Tahoma","sans-serif""> VinceRev=
[mailto:<a href=3D"mailto:vince.rev@gmail.com" target=3D"_blank">vince.rev=
@gmail.com</a>]
<br>
<b>Sent:</b> Tuesday, January 1, 2013 5:34 PM<br>
<b>To:</b> <a href=3D"mailto:std-proposals@isocpp.org" target=3D"_blank">st=
d-proposals@isocpp.org</a><br>
<b>Subject:</b> [std-proposals] A couple of classes for common mathematical=
constants ?<u></u><u></u></span></p>
</div>
</div><div><div class=3D"h5">
<p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
<p class=3D"MsoNormal">Hello.<br>
<br>
I find this very strange (maybe there are some good reasons that I don'=
t know) not to have common mathematical constants like pi in the standard a=
nd to be forced to use syntaxes like 4*arctan(1) to get the value of pi.<br=
>
<br>
So I have written a couple of very simple classes to solve that problem, pr=
esented in the joined image.<br>
1) numerical_constant, based on the same syntax as std::integral_constant<b=
r>
2) math_constants, based on the same syntax as numeric_limits<br>
<br>
Do you think that could be standardized ?<br>
<br>
Vincent<br>
=C2=A0<u></u><u></u></p>
<p class=3D"MsoNormal">-- <br>
=C2=A0<br>
=C2=A0<br>
=C2=A0<u></u><u></u></p>
</div></div></div>
</div>
</div>
<p></p>
-- <br>
=C2=A0<br>
=C2=A0<br>
=C2=A0<br>
</blockquote></div><br><br clear=3D"all"><br>-- <br>Vincent Reverdy<i><br>P=
hd Student @ Laboratory Universe and Theories</i><i><br>Cosmology and Gener=
al Relativity Group<br>Observatory of Paris-Meudon, France<br></i><span><i>=
</i></span>
</div>
<p></p>
-- <br />
<br />
<br />
<br />
--14dae93b5c727488b304d244ae5c--
.
Author: Marc <marc.glisse@gmail.com>
Date: Tue, 1 Jan 2013 23:06:33 -0800 (PST)
Raw View
------=_Part_428_25192169.1357110393543
Content-Type: text/plain; charset=ISO-8859-1
On Wednesday, January 2, 2013 3:05:33 AM UTC+1, VinceRev wrote:
>
> Yes, here it is (if you think that this design could be a good idea, I can
> write a proposal as a latex document of course). (+ I realized that 2 const
> were omitted in the image)
> ------------------------------------------------------------------------
> #include <iostream>
>
> // Numerical constant class, based on the model of std::integral_constant
> template<typename T>
> struct numerical_constant
> {
> typedef T value_type;
> typedef numerical_constant<T> type;
> constexpr numerical_constant(const value_type val): value{val} {}
> constexpr value_type operator()() const {return value;}
> constexpr operator value_type() const {return value;}
> template<class U> constexpr U cast() const {return value;} // To be
> discussed
> const value_type value;
> };
>
> // Common mathematical constants, based on the model of std::numeric_limits
> template<typename T>
> struct math_constants
> {
> static constexpr numerical_constant<T> pi =
> numerical_constant<T>(3.1415926535897932384626433832795028841971693993751058209749445L);
> // General: pi*
> *
>
If you make pi a function (look at numeric_limits more closely), is there
still any reason to use a wrapper like numerical_constant? Assuming we get
a BigFloat in the library, will I be able to ask for pi with 1234 digits
through the common interface somehow? Would making pi a type help?
--
------=_Part_428_25192169.1357110393543
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<br><br>On Wednesday, January 2, 2013 3:05:33 AM UTC+1, VinceRev wrote:<blo=
ckquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-=
left: 1px #ccc solid;padding-left: 1ex;"><div>Yes, here it is (if you think=
that this design could be a good idea, I can write a proposal as a latex d=
ocument of course). (+ I realized that 2 const were omitted in the image)<b=
r><span style=3D"font-family:courier new,monospace">-----------------------=
-------<wbr>------------------------------<wbr>------------<br>
#include <iostream><br><br>// Numerical constant class, based on the =
model of std::integral_constant<br>template<typename T><br>struct num=
erical_constant<br>{<br> typedef T value_type;<br> &=
nbsp; typedef numerical_constant<T> type;<br>
constexpr numerical_constant(const value_type val): valu=
e{val} {}<br> constexpr value_type operator()() const {re=
turn value;}<br> constexpr operator value_type() const {r=
eturn value;}<br> template<class U> constexpr U cas=
t() const {return value;} // To be discussed<br>
const value_type value;<br>};<br><br>// Common mathemati=
cal constants, based on the model of std::numeric_limits<br>template<typ=
ename T><br>struct math_constants<br>{<br> static cons=
texpr numerical_constant<T> pi &nb=
sp; =3D numerical_constant<T&g=
t;(3.<wbr>141592653589793238462643383279<wbr>502884197169399375105820974944=
<wbr>5L); // General: pi</span><i><br></i></div></blockquote><div> <br=
>If you make pi a function (look at numeric_limits more closely), is there =
still any reason to use a wrapper like numerical_constant? Assuming we get =
a BigFloat in the library, will I be able to ask for pi with 1234 digits th=
rough the common interface somehow? Would making pi a type help?<br></div>
<p></p>
-- <br />
<br />
<br />
<br />
------=_Part_428_25192169.1357110393543--
.
Author: John Bytheway <jbytheway@gmail.com>
Date: Wed, 02 Jan 2013 08:55:43 -0500
Raw View
On 01/01/13 20:34, VinceRev wrote:
> Hello.
>
> I find this very strange (maybe there are some good reasons that I don't
> know) not to have common mathematical constants like pi in the standard
> and to be forced to use syntaxes like 4*arctan(1) to get the value of pi.
>
> So I have written a couple of very simple classes to solve that problem,
> presented in the joined image.
> 1) numerical_constant, based on the same syntax as std::integral_constant
> 2) math_constants, based on the same syntax as numeric_limits
>
> Do you think that could be standardized ?
Have you looked at how Boost.Math deals with this? See
<http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_dist/html/math_toolkit/constants/intro.html>.
In particular, note that they provide for getting a more precise value
than can be represented with a built-in float type.
John Bytheway
--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Wed, 2 Jan 2013 16:19:45 +0200
Raw View
On 2 January 2013 15:55, John Bytheway <jbytheway@gmail.com> wrote:
> Have you looked at how Boost.Math deals with this? See
> <http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_dist/html/math_toolkit/constants/intro.html>.
> In particular, note that they provide for getting a more precise value
> than can be represented with a built-in float type.
Indeed. Walter Brown once mentioned that just having a long double
constant is not good enough,
so serious users need to control the size and accuracy for different
practical uses.
--
.
Author: VinceRev <vince.rev@gmail.com>
Date: Wed, 2 Jan 2013 08:50:28 -0800 (PST)
Raw View
------=_Part_488_8747271.1357145428985
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
*If you make pi a function (look at numeric_limits more closely), is there=
=20
still any reason to use a wrapper like numerical_constant? Assuming we get=
=20
a BigFloat in the library, will I be able to ask for pi with 1234 digits=20
through the common interface somehow? Would making pi a type help?*
*Indeed. Walter Brown once mentioned that just having a long double=20
constant is not good enough,=20
so serious users need to control the size and accuracy for different=20
practical uses. *
The wrapper can be usefull to define new numerical constants from an user=
=20
point of view. But if you want more precision you will have to define a=20
function (for example template<class T> pi()) that uses a convergent serie=
=20
(BBP formula for example) to precompute the constant to the desired=20
precision. So instead of this design, we could have, something like that:
---------------------------------------------------
#include <iostream>
// Numerical constant class, based on the model of std::integral_constant
template<typename T>
struct numerical_constant
{
typedef T value_type;
typedef numerical_constant<T> type;
constexpr numerical_constant(const value_type val): value{val} {}
constexpr value_type operator()() const {return value;}
constexpr operator value_type() const {return value;}
template<class U> constexpr U cast() const {return value;} // To be=20
discussed
const value_type value;
};
// Compute mathematical constants to the desired precision
struct compute_math_constants
{
template<class T>
static constexpr pi() {/* BBP formula */}
}
// Common mathematical constants, based on the model of std::numeric_limits
template<typename T>
struct math_constants
{
static constexpr numerical_constant<T> pi =3D=20
numerical_constant<T>(compute_math_constants::pi<T>()); //=20
General: pi
static constexpr numerical_constant<T> e =3D=20
numerical_constant<T>(compute_math_constants::e<T>()); //=20
General: Napier's constant
static constexpr numerical_constant<T> euler =3D=20
numerical_constant<T>(compute_math_constants::euler<T>()); //=20
General: Euler=E2=80=93Mascheroni constant
static constexpr numerical_constant<T> phi =3D=20
numerical_constant<T>(compute_math_constants::phi<T>()); //=20
General: golden ratio
static constexpr numerical_constant<T> pythagoras =3D=20
numerical_constant<T>(compute_math_constants::pythagoras<T>()); //=20
General: Pythagoras' constant, square root of 2
static constexpr numerical_constant<T> theodorus =3D=20
numerical_constant<T>(compute_math_constants::theodorus<T>()); //=20
General: Theodorus' constant, square root of 3
static constexpr numerical_constant<T> degree =3D=20
numerical_constant<T>(compute_math_constants::degree<T>()); //=20
General: value of a degree in radians
static constexpr numerical_constant<T> glaisher =3D=20
numerical_constant<T>(compute_math_constants::glaisher<T>()); //=20
Number theory: Glaisher=E2=80=90Kinkelin constant
static constexpr numerical_constant<T> khinchin =3D=20
numerical_constant<T>(compute_math_constants::khinchin<T>()); //=20
Number theory: Khinchin's constant
static constexpr numerical_constant<T> soldner =3D=20
numerical_constant<T>(compute_math_constants::soldner<T>()); //=20
Number theory: Ramanujan=E2=80=93Soldner constant
static constexpr numerical_constant<T> mertens =3D=20
numerical_constant<T>(compute_math_constants::mertens<T>()); //=20
Number theory: Meissel=E2=80=93Mertens constant
static constexpr numerical_constant<T> plastic =3D=20
numerical_constant<T>(compute_math_constants::plastic<T>()); //=20
Number theory: plastic number
static constexpr numerical_constant<T> catalan =3D=20
numerical_constant<T>(compute_math_constants::catalan<T>()); //=20
Combinatorics: Catalan's constant
static constexpr numerical_constant<T> feigenbaum_delta =3D=20
numerical_constant<T>(compute_math_constants::feigenbaum_delta<T>()); //=20
Chaos theory: first Feigenbaum constant
static constexpr numerical_constant<T> feigenbaum_alpha =3D=20
numerical_constant<T>(compute_math_constants::feigenbaum_alpha<T>()); //=20
Chaos theory: second Feigenbaum constant
static constexpr numerical_constant<T> omega =3D=20
numerical_constant<T>(compute_math_constants::omega<T>()); //=20
Mathematical analysis: Omega constant
};
// Examples
int main(int argc, char* argv[])
{
// Display some constants
std::cout<<"pi =3D "<<math_constants<double>::pi()<<"\n";
std::cout<<"e =3D "<<math_constants<double>::e()<<"\n";
std::cout<<"euler =3D "<<math_constants<double>::euler()<<"\n";
std::cout<<std::endl;
=20
// Create a new constant
numerical_constant<double> hbar(1.054571726E-34);
std::cout<<"hbar [double] =3D "<<hbar<<"\n";
std::cout<<"hbar [double] =3D "<<hbar()<<"\n";
std::cout<<"hbar [float] =3D "<<hbar.cast<float>()<<"\n";
std::cout<<std::endl;
return 0;
}
---------------------------------------------------
--=20
------=_Part_488_8747271.1357145428985
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<i>If you make pi a function (look at numeric_limits more closely), is=20
there still any reason to use a wrapper like numerical_constant?=20
Assuming we get a BigFloat in the library, will I be able to ask for pi=20
with 1234 digits through the common interface somehow? Would making pi a
type help?</i><br><br><i>Indeed. Walter Brown once mentioned that just hav=
ing a long double
<br>constant is not good enough,
<br>so serious users need to control the size and accuracy for different
<br>practical uses.
</i><br><br>The wrapper can be usefull to define new numerical constants fr=
om an user point of view. But if you want more precision you will have to d=
efine a function (for example template<class T> pi()) that uses a con=
vergent serie (BBP formula for example) to precompute the constant to the d=
esired precision. So instead of this design, we could have, something like =
that:<br><br><span style=3D"font-family: courier new,monospace;">----------=
-----------------------------------------<br>#include <iostream><br><=
br>// Numerical constant class, based on the model of std::integral_constan=
t<br>template<typename T><br>struct numerical_constant<br>{<br> =
typedef T value_type;<br> typedef numerical_=
constant<T> type;<br> constexpr numerical_constant(=
const value_type val): value{val} {}<br> constexpr value_=
type operator()() const {return value;}<br> constexpr ope=
rator value_type() const {return value;}<br> template<=
class U> constexpr U cast() const {return value;} // To be discussed<br>=
const value_type value;<br>};<br><br>// Compute mathemat=
ical constants to the desired precision<br>struct compute_math_constants<br=
>{<br> template<class T><br> stat=
ic constexpr pi() {/* BBP formula */}<br>}<br><br>// Common mathematical co=
nstants, based on the model of std::numeric_limits<br>template<typename =
T><br>struct math_constants<br>{<br> static constexpr =
numerical_constant<T> pi &nb=
sp; =3D numerical_constant<T>(com=
pute_math_constants::pi<T>()); &nb=
sp; // General: pi<br>  =
; static constexpr numerical_constant<T> e &nb=
sp; =3D n=
umerical_constant<T>(compute_math_constants::e<T>()); &nbs=
p; &=
nbsp; // General: Napier's constant<br> static constexpr =
numerical_constant<T> euler =
=3D numerical_constant<T>(compute_math_const=
ants::euler<T>()); &nb=
sp; // General: Euler=E2=80=93Mascheroni constant<br> &nbs=
p; static constexpr numerical_constant<T> phi =
=3D numerical_=
constant<T>(compute_math_constants::phi<T>());  =
; // General: g=
olden ratio<br> static constexpr numerical_constant<T&=
gt; pythagoras =3D numerical_constant&l=
t;T>(compute_math_constants::pythagoras<T>()); &n=
bsp; // General: Pythagoras' constant, square root of 2<br>&nbs=
p; static constexpr numerical_constant<T> theodorus =
=3D numerical_constant<T>(comput=
e_math_constants::theodorus<T>());  =
; // General: Theodorus' constant, square root of 3<br> &n=
bsp; static constexpr numerical_constant<T> degree &=
nbsp; =3D numerical_constant<T>(c=
ompute_math_constants::degree<T>()); &nb=
sp; // General: value of a degree in radians<br>&nb=
sp; static constexpr numerical_constant<T> glaisher =
=3D numerical_constant<T>(=
compute_math_constants::glaisher<T>()); =
// Number theory: Glaisher=E2=80=90Kinkelin constant<br>=
static constexpr numerical_constant<T> khinchin&nb=
sp; =3D numerical_constant<T&g=
t;(compute_math_constants::khinchin<T>()); &nb=
sp; // Number theory: Khinchin's constant<br> =
static constexpr numerical_constant<T> soldner &nbs=
p; =3D numerical_constant<T>(comp=
ute_math_constants::soldner<T>());  =
; // Number theory: Ramanujan=E2=80=93Soldner constant<br=
> static constexpr numerical_constant<T> mertens&nb=
sp; =3D numerical_constant&=
lt;T>(compute_math_constants::mertens<T>()); &nbs=
p; // Number theory: Meissel=E2=80=93Mertens =
constant<br> static constexpr numerical_constant<T>=
plastic =3D numerica=
l_constant<T>(compute_math_constants::plastic<T>());  =
; // Number theory: plastic numbe=
r<br> static constexpr numerical_constant<T> catala=
n =3D numerical_const=
ant<T>(compute_math_constants::catalan<T>()); =
// Combinatorics: Catalan's constant<b=
r> static constexpr numerical_constant<T> feigenbau=
m_delta =3D numerical_constant<T>(compute_math_constants::feigenbaum_=
delta<T>()); // Chaos theory: first Feigenbaum constant<br> &nbs=
p; static constexpr numerical_constant<T> feigenbaum_alpha =3D =
numerical_constant<T>(compute_math_constants::feigenbaum_alpha<T&g=
t;()); // Chaos theory: second Feigenbaum constant<br> st=
atic constexpr numerical_constant<T> omega &nb=
sp; =3D numerical_constant<T>(com=
pute_math_constants::omega<T>()); =
// Mathematical analysis: Omega constant<br>=
};<br><br>// Examples<br>int main(int argc, char* argv[])<br>{<br> &nb=
sp; // Display some constants<br> std::cout<<=
"pi =3D "<<math_constants<double>::pi()<&l=
t;"\n";<br> std::cout<<"e =
=3D "<<math_constants<double>::e()<<"\n";<br> =
std::cout<<"euler =3D "<<math_constants<double>::e=
uler()<<"\n";<br> std::cout<<std::endl;<br>&n=
bsp; <br> // Create a new constant<br> &=
nbsp; numerical_constant<double> hbar(1.054571726E-34);<br>&nbs=
p; std::cout<<"hbar [double] =3D "<<hbar<<"\n=
";<br> std::cout<<"hbar [double] =3D "<<hbar(=
)<<"\n";<br> std::cout<<"hbar [float] =
=3D "<<hbar.cast<float>()<<"\n";<br> st=
d::cout<<std::endl;<br> return 0;<br>}<br>---------=
------------------------------------------</span><br><br>
<p></p>
-- <br />
<br />
<br />
<br />
------=_Part_488_8747271.1357145428985--
.
Author: Beman Dawes <bdawes@acm.org>
Date: Wed, 2 Jan 2013 15:35:14 -0500
Raw View
On Tue, Jan 1, 2013 at 8:34 PM, VinceRev <vince.rev@gmail.com> wrote:
> Hello.
>
> I find this very strange (maybe there are some good reasons that I don't
> know) not to have common mathematical constants like pi in the standard and
> to be forced to use syntaxes like 4*arctan(1) to get the value of pi.
You can read the FAQ for the Boost constants to get an idea of the
difficulties involved. See
http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_dist/html/math_toolkit/constants/FAQ.html
> So I have written a couple of very simple classes to solve that problem,
> presented in the joined image.
> 1) numerical_constant, based on the same syntax as std::integral_constant
> 2) math_constants, based on the same syntax as numeric_limits
>
> Do you think that could be standardized ?
Without years of experience with a mature shipping library? Seems
doubtful, given the problems known to lurk in this problem domain. But
that's just my personal opinion.
--Beman
--
.
Author: VinceRev <vince.rev@gmail.com>
Date: Wed, 2 Jan 2013 15:54:37 -0800 (PST)
Raw View
------=_Part_66_33431940.1357170877511
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I am absolutely not pretending writing something of the quality of boost=20
constants and I know that a bunch of problems arises when you try to define=
=20
multiprecision floating point constants ;-)
My first idea was not to propose abritrary-precision constants simply=20
because arbitrary precision is not yet a part of the C++.
But as math_constants is a template class, there is a room for arbitrary=20
precision specialization in a future update.
Now, from a pure user point of view, I find it strange not to have pi in a=
=20
language as the C++ in 2013 and be forced to use 4*arctan(1).
Regards,
Vincent
Le mercredi 2 janvier 2013 21:35:14 UTC+1, Beman Dawes a =E9crit :
>
> On Tue, Jan 1, 2013 at 8:34 PM, VinceRev <vinc...@gmail.com <javascript:>=
>=20
> wrote:=20
> > Hello.=20
> >=20
> > I find this very strange (maybe there are some good reasons that I don'=
t=20
> > know) not to have common mathematical constants like pi in the standard=
=20
> and=20
> > to be forced to use syntaxes like 4*arctan(1) to get the value of pi.=
=20
>
> You can read the FAQ for the Boost constants to get an idea of the=20
> difficulties involved. See=20
>
> http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_dist/html/math_=
toolkit/constants/FAQ.html=20
>
> > So I have written a couple of very simple classes to solve that problem=
,=20
> > presented in the joined image.=20
> > 1) numerical_constant, based on the same syntax as=20
> std::integral_constant=20
> > 2) math_constants, based on the same syntax as numeric_limits=20
> >=20
> > Do you think that could be standardized ?=20
>
> Without years of experience with a mature shipping library? Seems=20
> doubtful, given the problems known to lurk in this problem domain. But=20
> that's just my personal opinion.=20
>
> --Beman=20
>
--=20
------=_Part_66_33431940.1357170877511
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I am absolutely not pretending writing something of the quality of boost co=
nstants and I know that a bunch of problems arises when you try to define m=
ultiprecision floating point constants ;-)<br>My first idea was not to prop=
ose abritrary-precision constants simply because arbitrary precision is not=
yet a part of the C++.<br>But as math_constants is a template class, there=
is a room for arbitrary precision specialization in a future update.<br><b=
r>Now, from a pure user point of view, I find it strange not to have pi in =
a language as the C++ in 2013 and be forced to use 4*arctan(1).<br><br>Rega=
rds,<br>Vincent<br><br><br>Le mercredi 2 janvier 2013 21:35:14 UTC+1, Beman=
Dawes a =E9crit :<blockquote class=3D"gmail_quote" style=3D"margin: 0=
;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Tue,=
Jan 1, 2013 at 8:34 PM, VinceRev <<a href=3D"javascript:" target=3D"_bl=
ank" gdf-obfuscated-mailto=3D"rFN_qOVgjWUJ">vinc...@gmail.com</a>> wrote=
:
<br>> Hello.
<br>>
<br>> I find this very strange (maybe there are some good reasons that I=
don't
<br>> know) not to have common mathematical constants like pi in the sta=
ndard and
<br>> to be forced to use syntaxes like 4*arctan(1) to get the value of =
pi.
<br>
<br>You can read the FAQ for the Boost constants to get an idea of the
<br>difficulties involved. See
<br><a href=3D"http://www.boost.org/doc/libs/1_52_0/libs/math/doc/sf_and_di=
st/html/math_toolkit/constants/FAQ.html" target=3D"_blank">http://www.boost=
..org/doc/libs/<wbr>1_52_0/libs/math/doc/sf_and_<wbr>dist/html/math_toolkit/=
<wbr>constants/FAQ.html</a>
<br>
<br>> So I have written a couple of very simple classes to solve that pr=
oblem,
<br>> presented in the joined image.
<br>> 1) numerical_constant, based on the same syntax as std::integral_c=
onstant
<br>> 2) math_constants, based on the same syntax as numeric_limits
<br>>
<br>> Do you think that could be standardized ?
<br>
<br>Without years of experience with a mature shipping library? Seems
<br>doubtful, given the problems known to lurk in this problem domain. But
<br>that's just my personal opinion.
<br>
<br>--Beman
<br></blockquote>
<p></p>
-- <br />
<br />
<br />
<br />
------=_Part_66_33431940.1357170877511--
.