-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
63 lines (56 loc) · 1.68 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonCalculate_released()
{
QString text = ui->lineEditFraction->text();
text.replace(QChar( ',' ) , QChar( '.' ) );
ui->lineEditFraction->setText(text);
convertFractionToBinary ();
}
void MainWindow::convertFractionToBinary()
{
bits = ui->spinBoxBits->value();
fraction_input = ui->lineEditFraction->text().toDouble();
qDebug() << "Fraction = " << qSetRealNumberPrecision( 24 ) << fraction_input;
double substraction = 0;
double remain = fraction_input;
QString bitVektor = "";
int registerValue = 0;
double calculatedValue = 0;
for (int i= 1;i <= bits;i++)
{
substraction = qPow(2,(-1 * i));
if (remain >= substraction)
{
remain = remain - substraction;
bitVektor.append("1");
registerValue += qPow(2,bits- i); // fixed recalc
calculatedValue += substraction;
}
else
{
bitVektor.append("0");
}
}
error = 100 - (calculatedValue * 100) / fraction_input;
ui->lineEditBitResult->setText(bitVektor);
ui->lineEditBitRegister->setText(QString::number(registerValue));
ui->labelError->setText(QString::number(calculatedValue));
ui->labelErrorPercent->setText(QString::number(error,'f',4));
}
void MainWindow::on_pushButtonReset_released()
{
ui->lineEditBitRegister->setText("");
ui->lineEditBitResult->setText("");
ui->lineEditBitRegister->setText("");
}