Numworks Epsilon  1.4.1
Graphing Calculator Operating System
print_float.h
Go to the documentation of this file.
1 #ifndef POINCARE_PRINT_FLOAT_H
2 #define POINCARE_PRINT_FLOAT_H
3 
4 #include <poincare/integer.h>
5 #include <assert.h>
6 
7 namespace Poincare {
8 
9 namespace PrintFloat {
10  /* The 'Mode' refers to the way to display float 'scientific' or 'auto'. The
11  * scientific mode returns float with style -1.2E2 whereas the auto mode
12  * tries to return 'natural' float like (0.021) and switches to scientific
13  * mode if the float is too small or too big regarding the number of
14  * significant digits. */
15  enum class Mode {
16  Decimal = 0,
17  Scientific = 1,
18  Default = 2
19  };
20  constexpr static int bufferSizeForFloatsWithPrecision(int numberOfSignificantDigits) {
21  // The wors case is -1.234E-38
22  return numberOfSignificantDigits + 7;
23  }
24  /* This function prints the integer i in the buffer with a '.' at the position
25  * specified by the decimalMarkerPosition. It starts printing at the end of the
26  * buffer and print from right to left. The integer given should be of the right
27  * length to be written in bufferLength chars. If the integer is to small, the
28  * empty chars on the left side are completed with '0'. If the integer is too
29  * big, the printing stops when no more empty chars are available without
30  * returning any warning.
31  * Warning: the buffer is not null terminated but is ensured to hold
32  * bufferLength chars. */
33  void printBase10IntegerWithDecimalMarker(char * buffer, int bufferLength, Integer i, int decimalMarkerPosition);
34 
35  constexpr static int k_numberOfPrintedSignificantDigits = 7;
36  constexpr static int k_numberOfStoredSignificantDigits = 14;
37 
38  /* We here define the buffer size to write the lengthest float possible.
39  * At maximum, the number has 15 significant digits so, in the worst case it
40  * has the form -1.99999999999999e-308 (15+7+1 char) (the auto mode is always
41  * shorter. */
42  constexpr static int k_maxFloatBufferLength = k_numberOfStoredSignificantDigits+7+1;
43  /* We here define the buffer size to write the lengthest complex possible.
44  * The worst case has the form
45  * -1.99999999999999e-308*e^(-1.99999999999999e-308*i) (14+14+7+1 char) */
46  constexpr static int k_maxComplexBufferLength = k_maxFloatBufferLength-1+k_maxFloatBufferLength-1+7+1;
47 
48  /* If the buffer size is too small to display the right number of significant
49  * digits, the function forces the scientific mode and cap the number of
50  * significant digits to fit the buffer. If the buffer is too small to
51  * display any float, the text representing the float is truncated at the end
52  * of the buffer.
53  * ConvertFloatToText return the number of characters that have been written
54  * in buffer (excluding the last \O character) */
55  template <class T>
56  int convertFloatToText(T d, char * buffer, int bufferSize, int numberOfSignificantDigits, Mode mode = Mode::Default);
57  template <class T>
58  static int convertFloatToTextPrivate(T f, char * buffer, int numberOfSignificantDigits, Mode mode);
59 }
60 
61 }
62 
63 #endif
void printBase10IntegerWithDecimalMarker(char *buffer, int bufferLength, Integer i, int decimalMarkerPosition)
Definition: print_float.cpp:16
#define T(x)
Definition: events.cpp:26
int convertFloatToText(T d, char *buffer, int bufferSize, int numberOfSignificantDigits, Mode mode=Mode::Default)
Definition: print_float.cpp:41