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

Mathf.Sin(Mathf.PI) returns non zero number #17449

Closed
KellyThomas opened this issue Mar 11, 2018 · 5 comments
Closed

Mathf.Sin(Mathf.PI) returns non zero number #17449

KellyThomas opened this issue Mar 11, 2018 · 5 comments
Labels

Comments

@KellyThomas
Copy link
Contributor

KellyThomas commented Mar 11, 2018

Godot version:
aeb1c67

OS/device including version:
Window 7

Issue description:
C# Godot's Mathf.Sin(Mathf.PI) returns a negative non zero number.

Two of the signature characteristics of the sine function are:

  • it will return zero when the parameter value is an integer multiple of pi
  • it will return a non-negative number when the parameter value is within the range zero to pi inclusive.

Steps to reproduce:
If we attach the following gdscript to a Node:

extends Node

func _ready():
    print("-GDSCRIPT-")
    print("PI: %.13f" % PI)
    print("sin(PI): %.13f" % sin(PI))
    print("sqrt(sin(PI)): %.13f" % sqrt(sin(PI)))

It will produce this output:

-GDSCRIPT-
PI: 3.1415926535900
sin(PI): 0.0000000000000
sqrt(sin(PI)): 0.0000000110660

But if we attach this c sharp to a Node:

using Godot;

public class TestNode : Node
{
    public override void _Ready()
    {
        GD.Print("-CSHARP: Mathf-");
        GD.Print($"Mathf.PI: {Mathf.PI:F13}");
        GD.Print($"Mathf.Sin(Mathf.PI): {Mathf.Sin(Mathf.PI):F13}");
        GD.Print($"Mathf.Sqrt(Mathf.Sin(Mathf.PI)): {Mathf.Sqrt(Mathf.Sin(Mathf.PI)):F13}");
    }
}

It will generate this output:

-CSHARP: Mathf-
Mathf.PI: 3.1415930000000
Mathf.Sin(Mathf.PI): -0.0000000874228
Mathf.Sqrt(Mathf.Sin(Mathf.PI)): NaN
@ghost
Copy link

ghost commented Mar 11, 2018

cc: @neikeq

@bruvzg
Copy link
Member

bruvzg commented Mar 11, 2018

Output looks OK, Mathf use single floats, and you shouldn't expect more then 7 digits precision, GDscript use double floats and should give 15-16 digits, but print output only 12 (there's PR to fix it #17316).

@KellyThomas
Copy link
Contributor Author

KellyThomas commented Mar 12, 2018

Hmm.. I guess your right.

The issue was exposed by porting some gdscript that assumed sin() returned positive for the range 0 to PI. First I was using System.Math and everything worked (also using doubles) but when I changed to Godot.Math then stuff started disappearing from my screen as they were assigned a position values of NaN.

(float)Math.Sin((float)Math.PI) is returning same as Mathf.Sin(Mathf.PI) so it does look like the best we can expect.

@bruvzg
Copy link
Member

bruvzg commented Mar 12, 2018

assumed sin() returned positive for the range 0 to PI

Single precision PI representation is bigger than PI (it's 3.1415927410, real PI is 3.1415926535...).

@akien-mga
Copy link
Member

That's not a bug, that's just how floating point precision works. You should never assume that any float or double operation will return a pure integer 0. 0 does not exist as the result of a floating point operation, you should always check that abs(<your operation>) < epsilon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants