Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
01/27/08 15:24
Read: times


 
#149980 - I'd like to disagree
Responding to: ???'s previous message
Esko Ilola said:
The root cause was the compiler assumptions over the constants. These are always treated as SIGNED integers if not told othervise. This also applies for intermediate calculation results as in that comparison.

with this statement of yours. I think that the compiler has seen the statement as an uint16*uint16 and has not promoted the integer result to 32 bit. So, it does a 16 bit compare; which is correct.

So, the following syntax would make it to work as expected.
...     
if  ( MAX_POWER_LOSS < (uint32_t)Current_meas * (Prereg_meas - Voltage_meas) ) {
        pwmvalue += (uint16_t)(Voltage_set + Voltage_off);
    }
...

Here I am assuming that Prereg_meas is again uint16; need not be, but the result is uint16. The typecast for Current_meas makes sure that there is a 32 bit multiply yielding a 32 bit result for the compiler to use in the compare operation. Similarly, when the compiler sees the typecast for Current_meas here, it should perform a 32 bit division.
...     
        pwmvalue = MAX_POWER_LOSS / (uint32_t)Current_meas;
...


Esko Ilola said:
This, of course, is highly compiler dependent issue.

I agree with this statement.


My ass is still sorry but it is saved ;)

Ass sorry but ass happy. That's what matters. ;)

I'm sure your post will save some more sorry esses.

Cheers
Jerson


List of 28 messages in thread
TopicAuthorDate
The art of typecasting            01/01/70 00:00      
   I'd like to disagree            01/01/70 00:00      
   two comments            01/01/70 00:00      
      Two comments on comments            01/01/70 00:00      
   The Art of correct Constants            01/01/70 00:00      
      int - number of bits unknown            01/01/70 00:00      
         Corrected Art of correct Constants            01/01/70 00:00      
         More assumptions            01/01/70 00:00      
         stdint.h            01/01/70 00:00      
            Or, in the absence of stdint.h...            01/01/70 00:00      
      Const vs #define            01/01/70 00:00      
         Opps!            01/01/70 00:00      
            which may be a reason to prefer #define over const            01/01/70 00:00      
               enum            01/01/70 00:00      
                  Varies            01/01/70 00:00      
                     Know Thy Stuff - enum            01/01/70 00:00      
                        You are right            01/01/70 00:00      
   Know Thy Stuff            01/01/70 00:00      
      And...            01/01/70 00:00      
         I'll pass(cal) on that one :)            01/01/70 00:00      
   Everything in C defaults to int, which is signed            01/01/70 00:00      
   Never overlook lint            01/01/70 00:00      
      before Steve says it...            01/01/70 00:00      
         Pascal            01/01/70 00:00      
            strong typing            01/01/70 00:00      
               why Ada never took off            01/01/70 00:00      
                  Wirth-less            01/01/70 00:00      
               Why ?            01/01/70 00:00      

Back to Subject List