-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.cpp
executable file
·145 lines (117 loc) · 2.78 KB
/
print.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
*
* This module makes the printing
*
*/
#include <cmath>
#include <QDebug>
#include <stdint.h>
#include "print.h"
#include "scicalc.h"
QString Print::sciPrint(double value)
{
int digits=scicalc::app()->dialogGeneralSettings->getDigits();
bool showZeros=scicalc::app()->dialogGeneralSettings->getTrailingZeros();
QString output;
//printf(" start sciPrint ...\n");
int exp; // 10 base of value
int exp3; // 1000 base of value
int64_t rndValue; // rounded value
int64_t intValue; // integer part of value
int64_t fracValue; // fractional part of value
int intDigits; // number of integer digits
int fracDigits; // number of fractional digits
// find the 10 base and 1000 base of the value
if(value!=0)
{
exp=(int)floor(log10(fabs(value)));
exp3=(int)floor(log10(fabs(value))/3);
}
else
{
exp=0;
exp3=0;
}
//qDebug() << "exp:" << exp;
//qDebug() << "exp3:" << exp3;
// transform the value to an integer format and round it
rndValue=(int64_t)round(value*pow10(digits-1-exp));
//qDebug() << "factor:" << pow10(digits-1-exp);
//qDebug() << "rndValue:" << rndValue;
// get number of integer digits
intDigits=exp-3*exp3+1;
//qDebug() << "intDigits:" << intDigits;
// get the integer part of value
intValue=rndValue/pow10(digits-intDigits);
// print integer part
//qDebug() << "intValue:" << intValue;
output=QString::number(intValue);
// get number of digits of fractional part
fracDigits=std::max(digits-intDigits, 0);
// get fractional part of value
fracValue=fabs(rndValue % (int64_t)pow10(fracDigits));
if(intDigits<digits && (fracValue!=0 || showZeros))
{
// we need some fractional digits
//System.out.print(".");
output+=".";
for(int64_t base=pow10(fracDigits-1); base>0; base/=10)
{
//qDebug() << "digit:" << (fracValue/base);
output+=QString::number(fracValue/base);
fracValue%=base;
//qDebug() << "fracValue:" << fracValue;
if(fracValue==0 && !showZeros)
{
break;
}
}
}
// print scientific prefix
switch(exp3)
{
case 0: break;
case 1: output+="k"; break;
case 2: output+="M"; break;
case 3: output+="G"; break;
case 4: output+="T"; break;
case 5: output+="P"; break;
case 6: output+="E"; break;
case -1: output+="m"; break;
case -2: output+="u"; break;
case -3: output+="n"; break;
case -4: output+="p"; break;
case -5: output+="f"; break;
case -6: output+="a"; break;
default:
{
//qDebug() << "EE-case";
//qDebug() << output;
output+="e" + QString::number(3*exp3);
//qDebug() << output;
break;
}
}
//printf("sciPrint ... complete\n");
return output;
}
long double Print::pow10(int n)
{
long double pow=1;
if(n>0)
{
for(int i=0; i<n; i++)
{
pow*=10;
}
}
else if(n<0)
{
n=-n;
for(int i=0; i<n; i++)
{
pow/=10;
}
}
return pow;
}