Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Modulo with double and int types

Rusty

Active Coder
Trying to generate a list of temperatures that increment by 0.2 degrees in even numbers. The first temperature in the list is the result of a separate function not shown. Always a float but could be even or odd.

The objective is to:
Trim the first generated temperature to one digit after the decimal point.
Test to see if even or odd.
If even, add 0.2 until temp reaches 100
If odd add 0.1 to get to even, then 0.2 thereafter up to 100 (boiling poing water C)

So in the example the list would become 71.72, 71.8, 72.0, 72.2..., 100.0

The compiler clearly does not allow the modulo operand with float and int types. And the code seems clumsy to me.

Advice?
Thank you

C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
double temp1, temp2, temp3 = 0;
temp1 = 71.72;
temp2 = (temp1 - trunc(temp1));
temp3 = (round(temp2*10))/10;

if((temp2 + temp3) % 2 == 0){
   temp2 = temp2 + temp3;
   printf("\ntemp is even %lf", temp2);
}
else{
   temp2 = temp2 + temp3 + 0.1;
   printf("\ntemp is odd %lf", temp2);
}

int count = 1;
do{
      temp2 += 0.2;
      count++;

   printf("\ncount = %d \ttemp = %.1lf", count, temp2);
}   while (temp1 <= 100);

}
 
Even and odd are properties that apply to integer numbers only, and not to float or double. The compiler is quite right to refuse the modulo operation here. This
Trim the first generated temperature to one digit after the decimal point.
Test to see if even or odd.
just makes no sense to me. How would something with one digit after the decimal point being even or odd ? Even if that one digit is zero it cannot be. What is the point of considering even and odd temperatures anyway ?
 
The larger program models a distillation process. Other parameters to the model are functions of temperature and pressure. The list will end up as a lookup table that I want to have an increment that includes a whole number for temperature.

I'm not faulting the compiler. I was looking for advice on how to achieve my stated goal, given my inexperience with programming (I'm self taught and trying to learn). It's not an assignment but a personal project.

Thanks for reading my post.

If anyone can suggest a method I'd appreciate it. I'm sure there's a way... just looking a little guidance.

What is the point of considering even and odd temperatures anyway ?
Edit:
Sorry... should have added: Some folks monitor process with whole number thermometers. Others (like me) use RTD (resistive thermal device) that are capable of 0.1 display with accuracy of 0.2.
 
Last edited:
Still searching... Thought there was some promise in fmod() function but no success.

Suppose I could do 'if else': is value == .1, .3, .5, etc but just seems so inelegant.

Maybe treat it as string?

Reason I'm posting is going down these avenues by trial and error take soooo much time, as a newb. A little direction goes a long way.
 
Oh I can tell you that even when not a newb, going somewhere you've not been before takes trial and sometimes a lot of time !

Now I see what you mean with odd and even. Indeed, testing for the individual values with if..else is not very elegant. Better would be to use the Python match statement. If you still long for using odd and even, you could multiply your one-decimal numbers by 10 and convert them to integers. Do your thing with modulo and whatever, and at the end divide by 10 again.
 
Thank your for considering my dilemma. I came up with nearly identical solution just minutes before I saw your reply. I'm committed to C for this endeavor but have been poking around Python. I'm a bit old now and use this learning to keep mentally agile... neuro-plasticity the aging psychologists call it. 🙂Thanks again!

C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
double temp1, temp2, temp3, temp4 = 0;
int x = 0;
temp1 = 71.02;
temp2 = (temp1 - trunc(temp1)); // = 0.72
temp3 = (round(temp2*10))/10;   // = 0.7
temp4 = trunc(temp1) + temp3;   // = 71.7
printf("\n temp 2 = %lf - temp3 = %lf - temp4 = %lf", temp2, temp3, temp4);

x = temp3 * 10;
if (x % 2 == 0) {
   temp4 += 0;
   }
else{
   temp4 += 0.1;
   }

int count = 1;
do{
      temp4 += 0.2;
      count++;

   printf("\ncount = %d \ttemp = %.1lf", count, temp4);
}   while (temp4 <= 100);

}
 
Same here. I am also "a bit old", been retired for some years, and like to think that keeping up with programming (together with playing piano), will help against getting really old 😁

Haha, not sure why I mentioned the Python match command ! I guess because so many here are doing Python, which does not have a switch command like normal languages. The Python guys resisted implementing switch for a long time, for reasons best known to themselves, and when they finally gave in they decided to give it another name, as a last act of defiance 🤣
 
Last edited by a moderator:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom