math equation in c

hi there, being trying for days now, but this math equation from perl does not tranlate the same in c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #!/usr/bin/perl -w -s
{

  $test1 = 0x47;
  $test2 = 0xFF;
  $test3 = 0x03;
  $test4 = 0x69;
  print "\n";
  print "$test1,$test2,$test3,$test4\n";
  
  $power = ($test2<<8) + $test1;
  
  $voltage = ($test4<<8) |$test3 ;
   print "$power,$voltage\n";
  $voltage = 123.6 + ($voltage - 27620) / 85 * 0.4;
  $power = 1.19 + 0.84 * (($power - 288.0) / 204.0);

  print "$power,$voltage\n";
  }


the c version for some reason does not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void setup() {

 Serial.begin(1200);
    Serial.println("<Arduino is ready>");
    
    byte j = 0x03;
    byte k = 0x69;
    byte r = 0xFF;
    byte m = 0x47;

byte voltage = 0;
byte power = 0;
voltage = ( k<< 8) | j;
Serial.print("voltage prequ ");
      Serial.println(voltage);  
//byte volt = voltage;
         //Serial.println(volt);         
  voltage= (123.6 + (voltage - 27620) / 85 * 0.2);
   Serial.print("voltage ");
       Serial.println(voltage);  
      // Serial.println (" ");
  power = (m<<8) + r;
       Serial.print("Power prequ  "); 
            Serial.println(power);  
            //volt = voltage;
           power = 1.19 + 0.84 * ((power - 288) / 204);
       Serial.print("power out  ");     
       Serial.println(power);  
}
void loop() {

}


it gives completely differnt answers to the same question and i do not understand why.. any help would be apreciated..
The values that you assign to voltage and power are bigger than what can be stored in a byte so you probably want to use a different type, such as double, for these variables.
Last edited on
Not positive at all, but it could be due to integer division. C and C++ will truncate the result if you divide an integer by an integer.
For example, your power variable is of type byte, which I assume is a typedef for unsigned char, an integer type. when you do (volage - 27620) / 85 and ((power - 288) / 244) you are doing integer division.

Edit; I also agree with what Peter87 said, that makes sense.
Last edited on
I tried your suggetion of double and it works, thank you very much but the numbers are rounded significantly.. is ther away to get it not so rounded

the perl will ouput this as a value

269.088235294118, power
120.865882352941 voltage

and c outputs this

voltage 122.60 voltage is fine
power out 0.70 but it would be nice if power was a little more accurate.. to at least .0000 as it measures in kwatts.

any suggestion would be welcome

thank you for your time
It may just be rounding the output once you print the number to the screen, not necessarily rounding the actual number. You can check this by trying to print to the console the output using printf as such:
printf("power out %.4f",power); (instead of .4 you can specify how many digits come after the decimal place in, .x = "x digits" after decimal place).

This may or may not be the issue...

(of course you need to #import <stdio.h> if your going to use printf)
Last edited on
thanks guatemala007 but appearently does not work on arduino platform... as I tried what you suggested and Printf is simply irgnored no worries.. thank you for you help and suggestion
Topic archived. No new replies allowed.