// MPLAB C18
// Test sur convertion Float to ASCII
// pieges sur arrondi
// Test_flot_2_string_130927.c_
#include "p18f26k22.h"
#define p18f26k22
#define Versus "Versus 130926 C18 "
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#define OSCILLATEUR_INTERNE
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF // CONFIG1H
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF // quartz 10Mhz
#endif
#pragma config IESO=OFF,PWRTEN=OFF,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,T3CMX=PORTC0,P2BMX=PORTC0,CCP3MX=PORTB5
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H
// Using Q=20Mhz Clock
#define CLOCK_FREQ 20000000L
#define FOSC 10000000
#ifndef Byte
#define Byte unsigned char
#endif
char str[32];
float x,y ,z;
long int d,e;
int a,b;
int k;
void main()
{
//=========Zone for short tests===================
x= 34.567;
a = (int)x;
b = ((int) (x * 1000)) % 1000;
k=sprintf(str,"%8d%1c%3d",a,46,b);
/* Problem if over 32.0
6C8 str " 34.-969"
6FC a 34
6FE b -969
*/
x= 32.767;
a = (int)x;
b = ((int) (x * 1000)) % 1000;
k=sprintf(str,"%8d%1c%3d",a,46,b);
/* here no problemo because 32*1000 < 32767
6C8 str " 32.766"
6FC a 32
6FE b 766
*/
x= 34.567;
d = (long int)x;
e = ((long int) (x * 1000.0)) % 1000;
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/* OK
6C8 str " 34.567"
6F4 d 34
6F8 e 567
*/
x= 1234567.876;
d = (long int)x;
e = ((long int) (x * 1000.0)) % 1000;
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/* Erreur in decimal part
6C8 str " 1234567.936"
6F4 d 1234567
6F8 e 936
*/
x= 1234567.876;
y= modf(x,&z); // <- used to separate entire part and decimal part see C18 Maths
d=(long int)z;
e=(long int)(y*1000.0);
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/* OK, only rounded at 1/1000 decimale
6C8 str " 1234567.875"
6F4 d 1234567
6F8 e 875
*/
while(1);