D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19712 - std.math.sin and cos cast float to real before evaluating.
Summary: std.math.sin and cos cast float to real before evaluating.
Status: RESOLVED DUPLICATE of issue 18559
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-01 12:33 UTC by Alex
Modified: 2021-01-09 22:34 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Alex 2019-03-01 12:33:09 UTC
import std.stdio;
import std.math;
import std.random;
import core.stdc.math;
import std.datetime;


void main()
{
	auto r = benchmark!(

()=> std.math.sin(uniform(0.0f,1.0f))
,
()=> core.stdc.math.sinf(uniform(0.0f,1.0f))

)(1000000);

	writefln("%s\n%s",r[0].usecs,r[1].usecs);
}

With gdc-8, this shows around a 4 times difference in speed between std.math.sin and core.stdc.math.sinf.
Around 1.5 times difference with dmd via dlang.org web interface

This is because std.math.sin(float) casts the argument to real and then evaluates it.
Apparently ldc does this better.
Comment 1 Seb 2019-03-01 12:52:32 UTC
> With gdc-8, this shows around a 4 times difference in speed between std.math.sin and core.stdc.math.sinf.

FYI: GDC has a separate bug tracker

https://bugzilla.gdcproject.org/describecomponents.cgi

> Apparently ldc does this better.

Not apparently, it's has the same speed as the C runtime:

https://run.dlang.io/is/4AweFj
Comment 2 Alex 2019-03-01 13:16:40 UTC
I put the bug here because it is caused by phobos. Ldc has added a version(ldc) section that removes the casts to real and implements sin and cos differently depending on precision.
if I can get around it by calling a different sin function in phobos, then I think it is a phobos bug, but there is some interaction with the compilers
Comment 3 Mathias LANG 2021-01-09 22:34:14 UTC
Duplicate of 18559

*** This issue has been marked as a duplicate of issue 18559 ***