printf seems doesn´t work well with Floating point

19 Jan 2011 . Edited: 19 Jan 2011

HI,

 

I´m working with a nokia 5110 lcd, I´m implementing a function for draw a line between 2 points.

The problem is  that when I tried a division the result only shows the integer part.

 

This is the code:

 

void drawLine(char initX,char initY,char endX, char endY) {
    char y;
    float m=0;
    int diffY,diffX;


    printf("A)  initX: %u  initY: %u  endX: %u  endY: %u\n",initX,initY,endX,endY);
    //slope is calculated
    m=(float)((endY-initY)/(endX-initX));
   
    printf("B) CEIL m: %f initX: %u  initY: %u  endX: %u  end Y:  %u \n",ceil(m),initX,initY,endX, endY);
    printf("C) FLOOR m: %f initX: %u  initY: %u  endX: %u  end Y:  %u \n",floor(m),initX,initY,endX, endY);
.
.
.
}

 

the result is

 

A)  initX: 1  initY: 1  endX: 7  endY: 47
B) CEIL m: 7.000000 initX: 1  initY: 1  endX: 7  end Y:  47 
C) FLOOR m: 7.000000 initX: 1  initY: 1  endX: 7  end Y:  47 

 

the rigth result in m is 7.66, but   B) and C)  show 7.00000.

I don´t know if the problem is the diuvision or the data type(%f) declared  in printf.

 

If someone can help I will really apreciate it.

 

Thanks in advance.

19 Jan 2011 . Edited: 19 Jan 2011

I solved it with this

 

 

void drawLine(char initX,char initY,char endX, char endY) {
    char y;
    float m=0,diffY,diffX;
    //int diffY,diffX;
   
    
    diffY=(float)(endY-initY);
    diffX=(float)(endX-initX);
    printf("A)  initX: %u  initY: %u  endX: %u  endY: %u\n",initX,initY,endX,endY);
    //slope is calculated
    m=(float)(diffY/diffX);
   
    printf("B) CEIL m: %f initX: %u  initY: %u  endX: %u  end Y:  %u \n",ceil(m),initX,initY,endX, endY);
    printf("C) FLOOR m: %f initX: %u  initY: %u  endX: %u  end Y:  %u \n",floor(m),initX,initY,endX, endY);

.
.
.
}

the result is

 

 

A)  initX: 1  initY: 1  endX: 44  endY: 47<LF>
B) CEIL m: 2.000000 initX: 1  initY: 1  endX: 44  end Y:  47 <LF>
C) FLOOR m: 1.000000 initX: 1  initY: 1  endX: 44  end Y:  47 

Bye.

19 Jan 2011

When you do arithmetic with integers, the result will be an integer, too. Ir doesn't matter that you store the result in a float, it has been truncated already.

19 Jan 2011

Hi Julian,

Hendrik gives the right answer. Your original code was almost right...

You did:-

    float m=0;
    int diffY,diffX;
    m=(float)((endY-initY)/(endX-initX));

but what you need to do is "cast" in the right place. In you above code the cast should come at the point of division...

    float m=0;
    int diffY,diffX;
    m = ((float)(endY-initY)) / ((float)(endX-initX));

You might also like to consider the case of "division by zero" and handle that potential pitfall before attempting to calculate m

[edit... add div by zero detection]

    float m=0;
    int diffY,diffX;

    if ( int temp = endX - initX != 0 ) {
        m = ((float)(endY-initY)) / ((float)(temp));
    }
    else {
        // whoops, division by zero detected, handle as required.
    }
15 Mar 2011

Julian,

I have one of these units I plan to try out shortly. I'm curious if you looked at any of the LCD code already posted, to see if it might work. I can't quite tell from all the posts on LCDs if that is the case. I'm interested in low power and sunlight readable, and looks like the best option - most TFTs and many other displays are useless in sunlight.

thanks, Tom

18 Mar 2011

Tom Kreyche wrote:

Julian,

I have one of these units I plan to try out shortly. I'm curious if you looked at any of the LCD code already posted, to see if it might work. I can't quite tell from all the posts on LCDs if that is the case. I'm interested in low power and sunlight readable, and looks like the best option - most TFTs and many other displays are useless in sunlight.

thanks, Tom

Tom,

I tried some of that libraries but they didn´t work for the nokia 5110, actually I had to modify a library written for arduino. I have published my files in case they can help you. The main file contains the functions for the nokia 5110, the other file contains just bitmap images for test purpose.

http://mbed.org/users/jaga1184/programs/nokia_5110_2/lo78ly

Unfortunatly I haven´t tested the lcd for low power or sunlight conditions.

I hope it can be helpful.