Error using operator %

Hi, i want use the operator % (modulo), but arduino shows a error.

Serial.println (int (ar[y]));
         float  armin = ar[y] % 1;
         Serial.println (armin);

Show this error:

exit status 1
invalid operands of types 'float' and 'int' to binary 'operator%'

You can't modulo with a float. Divide and subtract.

Delta_G:
You can't modulo with a float. Divide and subtract.

I have a float number like 18.34, and i only want 0.34. If i do 18.34 % 1, i obtain 0.34, right?

idontknownothing:
I have a float number like 18.34, and i only want 0.34. If i do 18.34 % 1, i obtain 0.34, right?

No, you don't because modulo doesn't work on floating point numbers. If you do that you get an error.

Why don't you use floor and then subtract?

Or since you're just trying to print the number just print it. Why do you need to break it up into pieces here?

Delta_G:
No, you don't because modulo doesn't work on floating point numbers. If you do that you get an error.

Why don't you use floor and then subtract?

Or since you're just trying to print the number just print it. Why do you need to break it up into pieces here?

int  armin = (ar[y] - int (ar[y])) * 100;
      Serial.println (armin+1);

It's done, thank you!

Edit: Yes, imagine the number is 18.34, é have to send to telescope the number 18 and the number 34, because 18 is ar hours and 34 is ar min.

One more time, thank you!

18.34 decimal degrees is not equal to 18 degrees and 34 minutes.

jremington:
18.34 decimal degrees is not equal to 18 degrees and 34 minutes.

I know. But if the star have ar coordenates llike 14 h 13m, i write 14.13 for better use and in the program e divide 14 and 13.

idontknownothing:
I know. But if the star have ar coordenates llike 14 h 13m, i write 14.13 for better use and in the program e divide 14 and 13.

Oh no, you should do that as two separate variables then. Or keep it in an int or a long without a decimal and just know which digits are which. Remember floats are approximations and sometimes funny things happen to numbers when one can’t be stored exactly in a float.

If you don’t believe me go calculate the powers of 2 with the pow() function which returns a float, but store the results in an int instead and print them. When you learn that 2 squared is 3 and 2 cubed is 7 you’ll understand what I mean about floats being imprecise.

To elaborate on Delta_G's suggestion to use an int

int position = 1413;

void setup()
{
  Serial.begin(9600);
  Serial.print("Degrees: "); Serial.println(position / 100);
  Serial.print("Minutes: "); Serial.println(position % 100);
}

void loop()
{
}

Why don't you use floor and then subtract?

@OP

void setup()
{
  Serial.begin(9600);
  float x = 18.34;
  int y = floor(x);
  x = x - y;
  Serial.println(x); //0.34
}

void loop() 
{
  
}

Thank you all for the replys. I'll change that and i'll put 2 variables int, 1 for hour and one for min. Thank you all again.

Do you have negative values?
If so, you need to treat them carefully - for instance floor (-18.34) is -19.

TolpuddleSartre:
Do you have negative values?
If so, you need to treat them carefully - for instance floor (-18.34) is -19.

I'm doing this:

      //for example, ar= 1834
      Serial.println (ar / 100);
      Serial.println (ar %100);
      //It will show 18
                          34

And it works properly

That won't work for negative values either

Why are you so stuck on storing those in a single variable. The two numbers need to be two variables. If you want a single handle for them then write a class or a struct to hold them. But cramming them into a single number like that is just asking for trouble later.

Well, i guess i have to create 2 variables... No problem.

Delta_G:
Oh no, you should do that as two separate variables then. Or keep it in an int or a long without a decimal and just know which digits are which. Remember floats are approximations and sometimes funny things happen to numbers when one can't be stored exactly in a float.

If you don't believe me go calculate the powers of 2 with the pow() function which returns a float, but store the results in an int instead and print them. When you learn that 2 squared is 3 and 2 cubed is 7 you'll understand what I mean about floats being imprecise.

All values that can be represented by a 16-bit int can be represented exactly by a float. The pow() function just happens not to return exact values in all cases, even with integer arguments.

(In fact, you can store all integers +/- 8388608 exactly in a float.)

The OP was trying to store two separate numbers one on each side of the decimal. Are you telling me you think that was OK? Or are you just being pedantic about what values work or don't work in a float? The OP wasn't trying to store integers.

Delta_G:
The OP was trying to store two separate numbers one on each side of the decimal. Are you telling me you think that was OK?

No, not in the way OP was trying to do it.

Or are you just being pedantic about what values work or don't work in a float? The OP wasn't trying to store integers.

My intent was to point out that using the pow() function to illustrate the imprecision of floats is misleading, because floats really can be more exact than most people think.

christop:
My intent was to point out that using the pow() function to illustrate the imprecision of floats is misleading, because floats really can be more exact than most people think.

My intent was just to show that what looks like 8 sometimes turns out to be 7. If you ran pow and kept the result in a float and printed it you'd see 8.00 for 2 cubed. But as an int it turns into 7. My point was just that it isn't always what it appears to be with floats.