Velocity Reviews > probleme with printf("TB %.1f",temperature)

probleme with printf("TB %.1f",temperature)

Ben Bacarisse
Guest
Posts: n/a

 11-02-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Edward A. Falk) writes:

> In article <0.d1652cda267bcbf32fcb.20121030153354GMT.87wqy82c (E-Mail Removed)>,
> Ben Bacarisse <(E-Mail Removed)> wrote:
>>Andre <(E-Mail Removed)> writes:
>>
>>> This explode my pic16f648 a memory!!
>>> I am using PICC Light ( as a retired I can't afford a pro )
>>> So does anyone know about a way to convert float to ascii without
>>> using printf??

>>
>>It's hard in general, but your example format -- %.1f -- is easier. One
>>way might be:
>>
>> double f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
>> printf("%d.%d\n", (int)(f10/10), (int)fabs(fmod(f10, 10)));

>
> That's a pretty good approach.

No it isn't! You made me look again and I saw a bug. Drat.

> You could also do this:
>
> int f10 = f * 10 + (f < 0 ? -0.5 : 0.5);
> printf("%d.%d\n", f10/10, abs(f10)%10);
>
> Assuming, of course, that -214748364.8 <= f <= 214748364.7

You have it too: for negative f but where f > -0.95 the sign gets lost
because there's no integer -0 (well, that's one way of putting it).

If one is prepared to loose the range (I was aiming for something that
could handle the full int range) I think, now, that I'd do:

double f2 = fabs(f + (f < 0 ? -0.05 : 0.05));
printf("%s%d.%d\n", f < 0 ? "-" : "", (int)f2, (int)(f2 * 10) % 10);

which is nice and simple. To get the full int range I'd do:

double f2 = fabs(f + (f < 0 ? -0.05 : 0.05));
printf("%s%d.%d\n", f < 0 ? "-" : "", (int)f2,
(int)(fmod(f2, 1) * 10) % 10);

which is only a little more fussy, bu given the context, fmod might not
be available.

--
Ben.

Ben
Guest
Posts: n/a

 11-02-2012
Andre skrev 2012-10-30 15:23:
> This explode my pic16f648 a memory!!
> I am using PICC Light ( as a retired I can't afford a pro )
> So does anyone know about a way to convert float to ascii without using
> printf??
> Andre

Another problem with PICC, at least was, is that you had to write your
own putch () function which their printf used for output. There is no
relevant stdout in a tiny microcontroller. Reading the PICC 8.83 manual,
they are actually references to stdout but i can't find any info about
what this stdout stream really is. Personally I usually write my own
IO-functions for and use sprintf if I want string formatting.

/Ben

Andre
Guest
Posts: n/a

 11-02-2012
Le 30/10/2012 15:23, Andre a écrit :
> This explode my pic16f648 a memory!!
> I am using PICC Light ( as a retired I can't afford a pro )
> So does anyone know about a way to convert float to ascii without using
> printf??
> Andre

int fInput;
int lWhole=0;
int ulPart=0;
char CC = ' ';
if ( fInput & 0x8000 ) {
fInput = ~fInput +1;
CC = '-';
}
lWhole = ( fInput & 0x0FF0) >>4;
ulPart = (fInput & 0x00F);
printf("\n\rTE[%c%d.%.1d]",CC,lWhole,ulPart);

Although not perfect, it fits my need.
buffering. So far So good..
Many thanks to everybody.
Andre