Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tau constant to math module #6536

Merged
merged 2 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/stdlib/doc/src/math.xml
Expand Up @@ -102,9 +102,20 @@ erf(X) = 2/sqrt(pi)*integral from 0 to X of exp(-t*t) dt.</pre>

<func>
<name name="pi" arity="0" since=""/>
<fsummary>A useful number.</fsummary>
<fsummary>Ratio of the circumference of a circle to its diameter.</fsummary>
<desc>
<p>A useful number.</p>
<p>Ratio of the circumference of a circle to its diameter.</p>
<p>Floating point approximation of mathematical constant pi.</p>
</desc>
</func>

<func>
<name name="tau" arity="0" since="OTP @OTP-18361@"/>
<fsummary>Ratio of the circumference of a circle to its radius.</fsummary>
<desc>
<p>Ratio of the circumference of a circle to its radius.</p>
<p>This constant is equivalent to a full turn when described in radians.</p>
<p>The same as <c>2 * pi()</c>.</p>
</desc>
</func>

Expand Down
6 changes: 4 additions & 2 deletions lib/stdlib/src/math.erl
Expand Up @@ -19,7 +19,7 @@
%%
-module(math).

-export([pi/0]).
-export([pi/0,tau/0]).

%%% BIFs

Expand Down Expand Up @@ -154,5 +154,7 @@ tanh(_) ->
%%% End of BIFs

-spec pi() -> float().

pi() -> 3.1415926535897932.

-spec tau() -> float().
tau() -> 6.2831853071795864.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion. Instead of writing out the full (yeah, I know) sequence of digits, I would put the value for pi in a constant and define both pi/0 and tau/0 in terms of it:

-define(PI, 3.1415926535897932).

pi() -> ?PI.

tau() -> 2 * ?PI.

You know, in case some day god decides to make math things simpler 😸

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't wanted to do it to avoid loosing precision when compiling this module on different machines. We can make math simpler, but IEEE 754 will always stand in our way.

9 changes: 7 additions & 2 deletions lib/stdlib/test/math_SUITE.erl
Expand Up @@ -28,15 +28,15 @@
init_per_testcase/2, end_per_testcase/2]).

%% Test cases
-export([floor_ceil/1, error_info/1]).
-export([floor_ceil/1, error_info/1, constants/1]).


suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,1}}].

all() ->
[floor_ceil, error_info].
[floor_ceil, error_info, constants].

groups() ->
[].
Expand All @@ -60,6 +60,11 @@ init_per_testcase(_Case, Config) ->
end_per_testcase(_Case, _Config) ->
ok.

constants(_Config) ->
3.1415926535897932 = math:pi(),
6.2831853071795864 = math:tau(),
ok.

floor_ceil(_Config) ->
MinusZero = 0.0/(-1.0),
-43.0 = do_floor_ceil(-42.1),
Expand Down