[solved] Simple multiplication but an incomprehensible result !?

Hi all,

I think i have a problem with a variable type, but I can't find the trick ! >:(

I have a basic multiplication to do, but the result is not the right one...

here are the variables that I declare :

    const uint8_t XstepsNbr = 200 ;
    const uint8_t YstepsNbr = 200 ; 
    float coilWidth = 6 ; 
    float wireDia = 0.25 ;
    uint16_t totalTurnsNbr = 100 ;
...
...

// calculated constants
    const float Xpitch = XthreadedRodPitch / XstepsNbr ;
    float XwireStepsNbr ;
    float XYratio  ;
    float YturnsPerLayer ; 
    uint16_t XstepsPerLayer ;
    uint16_t YstepsPerLayer  ; 
    float totalLayerNumber  ; 
    uint32_t YtotalSteps  ;

Here are the formula for computing these variables (and a bunch of serial.print to debug my problem...):

void updateCoilData (){

    XwireStepsNbr = wireDia / Xpitch ;
    XYratio = XwireStepsNbr / YstepsNbr ; 
    YturnsPerLayer = coilWidth / wireDia ; 
    XstepsPerLayer = XwireStepsNbr * YturnsPerLayer ; 
    YstepsPerLayer = YstepsNbr * YturnsPerLayer ;      
    totalLayerNumber = totalTurnsNbr / YturnsPerLayer ;
    YtotalSteps = YstepsPerLayer * totalLayerNumber ;
    //YtotalSteps = YstepsNbr * totalTurnsNbr ;


    Xcount = 0 ;  
    XtotalCount = 0 ; 
    Xdirection = HIGH ; // ====> DIRECTION !!!
    YstepsCount = 0 ;   // Count the number of Y steps done.
    Ydirection = LOW ;  // ====> DIRECTION !!!
    Serial.print("XwireStepsNbr : ");
    Serial.println(XwireStepsNbr,4);
    Serial.print("XYratio : ");
    Serial.println(XYratio,4);
    Serial.print("YturnsPerLayer : ");
    Serial.println(YturnsPerLayer,4);
    Serial.print("XstepsPerLayer : ");
    Serial.println(XstepsPerLayer,4);
    Serial.print("YstepsPerLayer : ");
    Serial.println(YstepsPerLayer,4);
    Serial.print("totalLayerNumber : ");
    Serial.println(totalLayerNumber,4);
    Serial.print("YtotalSteps : ");
    Serial.println(YtotalSteps,4);
}

And here is the result of this computing :

XwireStepsNbr : 62.5001 Ok :-\
XYratio : 0.3125 Ok :-\
YturnsPerLayer : 24.0000 Ok :-\
XstepsPerLayer : 113130 but... :o WTF :o How 62.5001 * 24.0000 can be equal to 113130 !? This must be 1500
YstepsPerLayer : 1022333 NO ! This must be 4800 !!!
totalLayerNumber : 4.1667 Ok
YtotalSteps : 10320123 NO ! This must be 20000 !!!

I suppose that it is a question of variable type but I don't understand this stuff ...

Thanks for your help :slight_smile:

First, you define a float, but give it an interger value, float coilWidth = 6 ;

Second you want the compiler to compute a value for you, but XthreadRodPitch is not defined: const float Xpitch = XthreadedRodPitch / XstepsNbr ;

As an exercise, change all numerics to float and see if that computes what you want.

Paul

What do you think the 4 in Serial.print(integer_value, 4) does?
Remove it and you'll see the values you expect.

Paul_KD7HB:
First, you define a float, but give it an interger value, float coilWidth = 6 ;

Second you want the compiler to compute a value for you, but XthreadRodPitch is not defined: const float Xpitch = XthreadedRodPitch / XstepsNbr ;

As an exercise, change all numerics to float and see if that computes what you want.

Paul

Ooops sorry, I made a mistake when I cut/paste the piece of code into the thread.
Of course, XthreadedRodPitch was defined

const float XthreadedRodPitch = 0.8 ;

oqibidipo:
What do you think the 4 in Serial.print(integer_value, 4) does?
Remove it and you'll see the values you expect.

OMG :blush: ... It doesn't works like in an Excel spreadsheet... I don't understand exactely why it ends up to this results, be I guess it is because with 4 additionnal digits, the reading address in memory is not the right one or something like that.

Anyway, my problem is solved. Thanks a lot ! :wink: