sanguino/cores/arduino/Print.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 Print.cpp - Base class that provides print() and println()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (c) 2008 David A. Mellis. All right reserved.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Modified 23 November 2006 by David A. Mellis
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 #include <stdlib.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #include <stdio.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 #include <string.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 #include <math.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #include "Arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 #include "Print.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 // Public Methods //////////////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 /* default implementation: may be overridden */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 size_t Print::write(const uint8_t *buffer, size_t size)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 size_t n = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 while (size--) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 n += write(*buffer++);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 size_t Print::print(const __FlashStringHelper *ifsh)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 const char PROGMEM *p = (const char PROGMEM *)ifsh;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 size_t n = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 while (1) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 unsigned char c = pgm_read_byte(p++);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 if (c == 0) break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 n += write(c);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 size_t Print::print(const String &s)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 size_t n = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 for (uint16_t i = 0; i < s.length(); i++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 n += write(s[i]);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 size_t Print::print(const char str[])
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 return write(str);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 size_t Print::print(char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 return write(c);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 size_t Print::print(unsigned char b, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 return print((unsigned long) b, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 size_t Print::print(int n, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 return print((long) n, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 size_t Print::print(unsigned int n, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 return print((unsigned long) n, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 size_t Print::print(long n, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 if (base == 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 return write(n);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 } else if (base == 10) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 if (n < 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 int t = print('-');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 n = -n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 return printNumber(n, 10) + t;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 return printNumber(n, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 return printNumber(n, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 size_t Print::print(unsigned long n, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 if (base == 0) return write(n);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 else return printNumber(n, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 size_t Print::print(double n, int digits)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 return printFloat(n, digits);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 size_t Print::println(const __FlashStringHelper *ifsh)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 size_t n = print(ifsh);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 size_t Print::print(const Printable& x)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 return x.printTo(*this);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 size_t Print::println(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 size_t n = print('\r');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 n += print('\n');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 size_t Print::println(const String &s)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 size_t n = print(s);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 size_t Print::println(const char c[])
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 size_t n = print(c);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 size_t Print::println(char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 size_t n = print(c);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 size_t Print::println(unsigned char b, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 size_t n = print(b, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 size_t Print::println(int num, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 size_t n = print(num, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 size_t Print::println(unsigned int num, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 size_t n = print(num, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 size_t Print::println(long num, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 size_t n = print(num, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 size_t Print::println(unsigned long num, int base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 size_t n = print(num, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 size_t Print::println(double num, int digits)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 size_t n = print(num, digits);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 size_t Print::println(const Printable& x)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 size_t n = print(x);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 n += println();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 // Private Methods /////////////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 size_t Print::printNumber(unsigned long n, uint8_t base) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 char *str = &buf[sizeof(buf) - 1];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 *str = '\0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 // prevent crash if called with base == 1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 if (base < 2) base = 10;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 do {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 unsigned long m = n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 n /= base;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 char c = m - base * n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 *--str = c < 10 ? c + '0' : c + 'A' - 10;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 } while(n);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 return write(str);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 size_t Print::printFloat(double number, uint8_t digits)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 size_t n = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 // Handle negative numbers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 if (number < 0.0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 n += print('-');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 number = -number;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 // Round correctly so that print(1.999, 2) prints as "2.00"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 double rounding = 0.5;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 for (uint8_t i=0; i<digits; ++i)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 rounding /= 10.0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 number += rounding;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 // Extract the integer part of the number and print it
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 unsigned long int_part = (unsigned long)number;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 double remainder = number - (double)int_part;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 n += print(int_part);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 // Print the decimal point, but only if there are digits beyond
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 if (digits > 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 n += print(".");
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 // Extract digits from the remainder one at a time
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 while (digits-- > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 remainder *= 10.0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 int toPrint = int(remainder);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 n += print(toPrint);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 remainder -= toPrint;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 return n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 }

mercurial