• AVR Freaks

Helpful ReplyHot!Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20

Author
thecockroach
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2019/07/18 06:47:32
  • Location: 0
  • Status: offline
2019/07/19 08:24:47 (permalink)
0

Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20

Hi, i'm working with PIC18F45k20, and I have diferents problems not with the uc, more with de XC8 compiler.
 
1. The sprintf function always solve my life with liquid display 16x2, but I cant use that function because uses 60 % more program memory, somebody have any similar function to sprintf more eficient?
 
2. If I need to show a float number I have to do this:
 
float var = 12.3;
char numb[10];
decimal = (var - (int)var)*10;
itoa(numb, (int8_t) var, 10);
Lcd_Out(2, 0, numb);
Lcd_Out(2, 2, ".");
itoa(numb, decimal, 10);
Lcd_Out2(2, 3, numb);
 
I can not belive there is another way.
 
3. When I show this float number (12.3), shows me 12.2, I tested different values and 50% of cases show me 0.1 minus of the real number. I use the debuger of pickit3 and check what value have decimal variable and have the number 3 (in the case of 12.3), but shows the number 2. I'm going crazy.
 
Thanks in advance
#1
katela
Super Member
  • Total Posts : 1411
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 10:02:19 (permalink) ☄ Helpfulby ahuramazda 2019/07/19 10:59:42
+2 (2)
You could try the itoa() function. If you have x.xxx, you could multiply by 1000 to get xxxx then use itoa() to convert to string then parse the string to include a dot in front of 3rd character counting from right.
You can read more from this thread: https://www.microchip.com/forums/m68967.aspx and https://www.microchip.com/forums/m69532.aspx
 
 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#2
NKurzman
A Guy on the Net
  • Total Posts : 17846
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 10:20:21 (permalink) ☄ Helpfulby ahuramazda 2019/07/19 10:59:31
+2 (2)
This goes deeper that you think.  Floats are Not always exact values.  Is 12.3 stored as 12.3, or 12.29999999?
doing Math on floats can give you additional issues.  It is why I like to use integers.  ie store 12.3  as 123 knowing it has a decimal.  As for your rounding issue, add 0.05 to the Number before printing.  That will round it up.  Otherwise the math will truncate, which will appear as a rounding down.
#3
1and0
Access is Denied
  • Total Posts : 9762
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 11:39:05 (permalink)
+1 (1)
thecockroach
2. If I need to show a float number I have to do this:
 
float var = 12.3;
char numb[10];
decimal = (var - (int)var)*10;
itoa(numb, (int8_t) var, 10);
Lcd_Out(2, 0, numb);
Lcd_Out(2, 2, ".");
itoa(numb, decimal, 10);
Lcd_Out2(2, 3, numb);
 
I can not belive there is another way.
 
3. When I show this float number (12.3), shows me 12.2, I tested different values and 50% of cases show me 0.1 minus of the real number. I use the debuger of pickit3 and check what value have decimal variable and have the number 3 (in the case of 12.3), but shows the number 2. I'm going crazy.

Even with 24-bit floating point number, the closest representation to 12.3 is 12.3000488; so I doubt you're getting 12.2. ;)
 
As for the crazy part, give this a read:  https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
 
#4
1and0
Access is Denied
  • Total Posts : 9762
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 12:37:07 (permalink)
+1 (1)
thecockroach
3. When I show this float number (12.3), shows me 12.2, I tested different values and 50% of cases show me 0.1 minus of the real number. I use the debuger of pickit3 and check what value have decimal variable and have the number 3 (in the case of 12.3), but shows the number 2. I'm going crazy.
 

Reading your post closely. It seems you are saying the decimal variable = 3 but it is shown as the number 2.  Then perhaps it is your Lcd_Out2() function that is buggy.?!
 
Edit: What data type is the "decimal" variable?
post edited by 1and0 - 2019/07/19 12:41:45
#5
NKurzman
A Guy on the Net
  • Total Posts : 17846
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 17:11:42 (permalink)
0
1and0
Even with 24-bit floating point number, the closest representation to 12.3 is 12.3000488; so I doubt you're getting 12.2. ;)



Is C required to round up?  Is it possible it is rounding down?
#6
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11340
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 17:26:44 (permalink) ☄ Helpfulby thecockroach 2019/07/22 11:25:36
+2 (2)
C only rounds down.  (int)1.999 == 1.
#7
1and0
Access is Denied
  • Total Posts : 9762
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 17:30:54 (permalink)
+1 (1)
NKurzman
Is C required to round up?  Is it possible it is rounding down?


That would be a very good question for the language standard gurus.
 
I just now checked OP's code with XC8 v1.45 Free mode in MPLAB 8.92 and got 12.3.
 
FYI:
 
Using 24-bit float, the two closest representations are
 
  12.2998046875 - 0.0001953125
  12.3000488281 + 0.0000488281
 
For 32-bit float, they are
 
  12.2999992371 - 0.0000007629
  12.3000001907 + 0.0000001907
#8
1and0
Access is Denied
  • Total Posts : 9762
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/19 17:34:08 (permalink)
+1 (1)
jtemples
C only rounds down.  (int)1.999 == 1.

I think NK was asking about floating point number representation. For example, is 12.3 being represented as 0x4144CCCC or 0x4144CCCD in 32-bit floating point (or 0x4144CC or 0x4144CD in 24-bit floating point)?
 
Edit: XC8 v1.45 Free mode uses 0x4144CD (12.3000488281) and 0x4144CCCD (12.3000001907).
post edited by 1and0 - 2019/07/19 17:41:12
#9
mlp
boots too small
  • Total Posts : 805
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: Program memory problem with sprintf, float point, XC8 Compiler PIC18F45k20 2019/07/21 20:51:06 (permalink)
0
1and0
NKurzman
Is C required to round up?  Is it possible it is rounding down?

That would be a very good question for the language standard gurus.

C90 leaves the implementation of floating point types up to the compiler implementer, so one might like to think XC8's behaviour is documented in the manual, though I don't recall seeing this particular detail covered. (And 24 bit float doesn't meet C90's requirement for minimum precision anyway.)
 
C99 introduces explicit reliance on (the updated version, whose number escapes me, of) IEEE754 which delegates such behaviour mandates to the FP standard.

Mark (this opinion available for hire)
#10
Jump to:
© 2019 APG vNext Commercial Version 4.5