aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2024-03-12 14:16:47 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2024-03-12 14:16:47 +0600
commit6c2e211ae85d7de3d7f027e4aeaf382c34323097 (patch)
tree446a3682aca98aca2a0f1e29fb7606c81389d41c
parent8808dbe579cb4a935e32b3abfb2de81cf796e977 (diff)
downloadentropy-calc-6c2e211ae85d7de3d7f027e4aeaf382c34323097.tar.gz
entropy-calc-6c2e211ae85d7de3d7f027e4aeaf382c34323097.zip
Implement gmp wrapper for calculations
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r--headers/baseElement.h67
-rw-r--r--main.cpp8
2 files changed, 42 insertions, 33 deletions
diff --git a/headers/baseElement.h b/headers/baseElement.h
index 160d4b1..42fb8e5 100644
--- a/headers/baseElement.h
+++ b/headers/baseElement.h
@@ -3,20 +3,32 @@
#include <iostream>
#include <math.h>
+#ifdef _USE_GMP
+# include <gmp.h>
+# include <gmpxx.h>
+#endif // _USE_GMP
#include "readElementState.h"
using namespace std;
+#ifdef _USE_GMP
+ typedef mpf_class bigfloat;
+# define bigfloat_unwrap(X) (bigfloat(X)).get_d()
+#else
+ typedef double bigfloat;
+# define bigfloat_unwrap(X) (X)
+#endif // _USE_GMP
+
class baseElement
{
private:
string elementName;
- double latentHeatOfFusion;
- double latentHeatOfVaporization;
- double specificHeatSolid;
- double specificHeatLiquid;
- double specificHeatGas;
- double meltingPoint;
- double boilingPoint;
+ bigfloat latentHeatOfFusion;
+ bigfloat latentHeatOfVaporization;
+ bigfloat specificHeatSolid;
+ bigfloat specificHeatLiquid;
+ bigfloat specificHeatGas;
+ bigfloat meltingPoint;
+ bigfloat boilingPoint;
string initialState = "";
string finalState = "";
@@ -32,15 +44,15 @@ protected:
public:
string getElementName() { return elementName; }
- double getLatentHeatOfFusion() { return latentHeatOfFusion; }
- double getLatentHeatOfVaporization() { return latentHeatOfVaporization; }
- double getSpecificHeatSolid() { return specificHeatSolid; }
- double getSpecificHeatLiquid() { return specificHeatLiquid; }
- double getSpecificHeatGas() { return specificHeatGas; }
- double getMeltingPoint() { return meltingPoint; }
- double getBoilingPoint() { return boilingPoint; }
+ bigfloat getLatentHeatOfFusion() { return latentHeatOfFusion; }
+ bigfloat getLatentHeatOfVaporization() { return latentHeatOfVaporization; }
+ bigfloat getSpecificHeatSolid() { return specificHeatSolid; }
+ bigfloat getSpecificHeatLiquid() { return specificHeatLiquid; }
+ bigfloat getSpecificHeatGas() { return specificHeatGas; }
+ bigfloat getMeltingPoint() { return meltingPoint; }
+ bigfloat getBoilingPoint() { return boilingPoint; }
- double totalHeatNeeded(double mass, double fromTemp, double toTemp)
+ bigfloat totalHeatNeeded(double mass, double fromTemp, double toTemp)
{
if (fromTemp < 0 || toTemp < 0)
{
@@ -48,7 +60,7 @@ public:
return 0;
}
- double totalHeat = 0;
+ bigfloat totalHeat = 0;
if (fromTemp == meltingPoint || fromTemp == boilingPoint)
{
cout << "What is the state of the element at the initial temperature?" << endl;
@@ -99,7 +111,7 @@ public:
return totalHeat;
}
- double totalEntropyChange(double mass, double fromTemp, double toTemp)
+ bigfloat totalEntropyChange(double mass, double fromTemp, double toTemp)
{
if (fromTemp < 0 || toTemp < 0)
{
@@ -107,9 +119,7 @@ public:
return 0;
}
- double totalEntropy = 0;
- string initialState = "";
- string finalState = "";
+ bigfloat totalEntropy = 0;
if (fromTemp == meltingPoint || fromTemp == boilingPoint)
{
cout << "What is the state of the element at the initial temperature?" << endl;
@@ -125,42 +135,41 @@ public:
if (toTemp <= meltingPoint)
{
- totalEntropy += mass * specificHeatSolid * log(toTemp / fromTemp);
+ totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(toTemp / fromTemp));
if (toTemp == meltingPoint && finalState != "Solid")
totalEntropy += mass * latentHeatOfFusion / meltingPoint;
}
else if (toTemp <= boilingPoint)
{
if (fromTemp < meltingPoint)
- totalEntropy += mass * specificHeatSolid * log(meltingPoint / fromTemp);
+ totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp));
if (fromTemp == meltingPoint && initialState != "Liquid")
totalEntropy += mass * latentHeatOfFusion / meltingPoint;
if (fromTemp <= meltingPoint)
- totalEntropy += mass * specificHeatLiquid * log(toTemp / meltingPoint);
+ totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / meltingPoint));
else
- totalEntropy += mass * specificHeatLiquid * log(toTemp / fromTemp);
+ totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / fromTemp));
if (toTemp == boilingPoint && finalState != "Liquid")
totalEntropy += mass * latentHeatOfVaporization / boilingPoint;
}
else
{
if (fromTemp < meltingPoint)
- totalEntropy += mass * specificHeatSolid * log(meltingPoint / fromTemp);
+ totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp));
if (fromTemp == meltingPoint && initialState != "Liquid")
totalEntropy += mass * latentHeatOfFusion / meltingPoint;
if (fromTemp < boilingPoint)
- totalEntropy += mass * specificHeatLiquid * log(fromTemp / meltingPoint);
+ totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(fromTemp / meltingPoint));
if (fromTemp == boilingPoint && initialState != "Gas")
totalEntropy += mass * latentHeatOfVaporization / boilingPoint;
if (fromTemp > boilingPoint)
- totalEntropy += mass * specificHeatGas * log(toTemp / fromTemp);
+ totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / fromTemp));
else
- totalEntropy += mass * specificHeatGas * log(toTemp / boilingPoint);
+ totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / boilingPoint));
}
return totalEntropy;
}
};
-
#endif // BASEELEMENT_H_INCLUDED
diff --git a/main.cpp b/main.cpp
index a60839a..e9ebc81 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,7 +1,7 @@
+#define _USE_GMP
#include <iostream>
#include <vector>
-#include "headers/allElements.h"
-#include "headers/baseElement.h"
+#include "allElements.h"
using namespace std;
@@ -33,8 +33,8 @@ int main()
return 0;
}
- double totalHeat = elements[choice - 1]->totalHeatNeeded(mass, fromTemp, toTemp);
- double totalEntropy = elements[choice - 1]->totalEntropyChange(mass, fromTemp, toTemp);
+ double totalHeat = bigfloat_unwrap(elements[choice - 1]->totalHeatNeeded(mass, fromTemp, toTemp));
+ double totalEntropy = bigfloat_unwrap(elements[choice - 1]->totalEntropyChange(mass, fromTemp, toTemp));
cout << "The total heat needed is: " << totalHeat << " J" << endl;
cout << "The total entropy change is: " << totalEntropy << " J/K" << endl;
return 0;