Arduino Trigonometric functions rounding up to 2 digits (x.xx) is this normal?

Hi there

I building a code to calculate the uF needed for correcting a power factor.

The thing is that using acos, and tan, rounds the numbers

for example

double cosine = cos(2); // approximately -0.41614685058

returns -0.42

I tried float, double, converting DEG to RAD, etc.

Also used the integrated AVR library for math functions and the math.h library. Both yield same results

I´m using arduino Uno and Nano configurations

Is this hardwire in the code or there is something that can be done? Or should I buy a DUE?

Regards

Serial.print defaults to two digits after the decimal. See the docs for how to change it...

@Fabius_rex,

you did not provide proof :wink: You can not look in the processor to see what it does :smiley:

If you had provided a complete example, we possibly could have at least been able to reproduce or have pointed out your mistake or the compiler's mistake or the Arduino mistake.

But I think that westfw has put you on the right track.

PS double on AVR is the same as float; you will not gain anything by using double.

void setup() {

  double cosine = cos(2); // approximately -0.41614685058

  Serial.begin(9600);

  char buffr[30];
  dtostrf(cosine, 15, 7, buffr);

  Serial.println(buffr);
}

void loop() {
  
}

prints

     -0.4161468

in the terminal window :wink:

Huge thanks guys. Sorry I missed the serial print in the embedded code.

I shall give it a try later in the evening and came here but it's seems for sure that I was ignoring how serial print behaves for decimal
Thanks again and thanks for the fast reply

void setup() {
  Serial.begin(9600);
  Serial.println (cos(2), 7);
}
void loop()
{}

Suggestions worked great, however now I have another problem

whenever I try to make a sq() function it does not yield the correct result

Examples tryied

float subdiv; //tried INT also, but it is float because it will imply PI later
subdiv = 380*380; this is V (voltage) square, tryied sq(380) and 380*380 and both yield 13328
float coseno9;
float coseno6;
coseno9 = acos(0.9);
coseno6 = acos(0.6)
Serial.print("Coseno9: ");Serial.println(coseno9,7);
Serial.print("Coseno6: ");Serial.println(coseno6,7);
Serial.print("Subdiv: ");Serial.println(subdiv); //Returns 13328 instead of 144.400
}

1

subdiv = 380*380; this is V (voltage) square, tryied sq(380) and 380*380 and both yield 13328

The default is 16 bit signed integer math, with maximum value 32767. Since subdiv is a float, use the explicit decimal representation:

subdiv = 380.0*380.0;

jremington:

subdiv = 380*380; this is V (voltage) square, tryied sq(380) and 380*380 and both yield 13328

The default is 16 bit signed integer math, with maximum value 32767. Since subdiv is a float, use the explicit decimal representation:

subdiv = 380.0*380.0;

Thanks!