-
Notifications
You must be signed in to change notification settings - Fork 1
/
rprintff.c
89 lines (81 loc) · 1.92 KB
/
rprintff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* $Id: rprintff.c,v 1.3 2010/06/15 00:48:59 clivewebster Exp $
*
* Revision History
* ================
* $Log: rprintff.c,v $
* Revision 1.3 2010/06/15 00:48:59 clivewebster
* Add copyright license info
*
* Revision 1.2 2009/11/02 19:02:47 clivewebster
* Added revision log
*
* ===========
*
* Copyright (C) 2010 Clive Webster ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* rprintff.c
*
* implements rprintf float
* Created on: 15-Mar-2009
* Author: Clive Webster
*/
#include "rprintf.h"
#include "libdefs.h"
#include <stdarg.h>
// floating-point print
void rprintfFloat(char numDigits, double x)
{
unsigned char firstplace = FALSE;
unsigned char negative;
unsigned char i, digit;
double place = 1.0;
// save sign
negative = (x<0);
// convert to absolute value
x = (x>0)?(x):(-x);
// find starting digit place
for(i=0; i<15; i++)
{
if((x/place) < 10.0)
break;
else
place *= 10.0;
}
// print polarity character
if(negative)
rprintfChar('-');
else
rprintfChar('+');
// print digits
for(i=0; i<numDigits; i++)
{
digit = (x/place);
if(digit | firstplace | (place == 1.0))
{
firstplace = TRUE;
rprintfChar(digit+0x30);
}
else
rprintfChar(' ');
if(place == 1.0)
{
rprintfChar('.');
}
x -= (digit*place);
place /= 10.0;
}
}