Juce sin(float) to math.h sin(double)

I am quite new in C++ so I am not sure how to do that. But there is in Juce sinus function, and according to Juce documentation it takes and return only float values:
sin(float value);

But I want to use double and its precision.

As I know in <math.h> library there is also sinus function that takes and return double values, and have exactly the same syntax:
sin(double value);

But in Juce there is automatically included library with sin(float), so if I just:
#include <math.h>

Then I am not sure which sinus my code will use. But I want to be sure that all values (argument of sinus and its return) will be with double precision. How to do that?

I know that we can use standard library like that: std::function... but I am not sure how to use math.h library. I tried math.h::sin(double value) but it doesn’t work.

Of course I know I can use double values in Juce sin function. But I am afraid the returned values will be float.

Your problem is that JUCE client code is all automatically in the juce::
namespace, and math.h's functions, being C functions, have no concept of name spacing - to C++, it’s all in the global namespace (along with any definitions made by JUCE).

To be honest, I’m not sure where JUCE would be defining a sine function, I feel like if it is it’s probably just a typedef or an inline pass-through - but that’s neither here nor there, since you can (and should) use std:: namespace functions to be absolutely sure. std::sin(double val) will always return a double, std::sinf(float val) will always return a float. Use those, they’re in <cmath>.


1 Like

Thanks for reply

…I’m struggling to understand which “juce sin” function you’re talking about, because AFAIK we’ve never had one like you’re describing!

Do you mean juce::dsp::FastMathApproximations::sin ??

Hello, thanks for reply,
Actually I am not sure which sin function exactly I was using. I’ve just tried to check it, but there is no problem anymore. I don’t know why. Now when I start writing sin, xCode prompts more versions with double argument and float argument. But before there was only with float argument. I can’t now arrange exactly the same situation to check it again. But by the way, there is no problem for me anymore. But thanks for try to help.

Well, the rule is easy: in C++, you should always use std::sin()

1 Like

Never use C headers, they are never what you should be using in C++. Check on a web site like cppreference for the proper ones.