Source code: C++ Temperature Conversion and Vapor Pressure Deficit (VPD) šŸ› 

This is great. Definitely going to have a play with this at the weekend. Was thinking about writing something like this for my app, would definitely be a good start to get my head around the intricacies of VPD

2 Likes

Seeing folk finding use for some of this stuff is encouraging toward future works. Thank you.

:pray: :pray: :pray:

2 Likes

Iā€™ve made several updates to the temperature conversion classes:

  • Added Rankine units.
  • Set the base class to Celcius such that any mathematical operations using the temperature types are always performed in Celcius and then converted back to the target temperature unit. Math with temperature, in some cases, is nonsensical since we should be talking temperature deltas. Though, as a class, mathematical formulas can utilize the conversions allowing for flexibility. Just be aware that additions between two different units will always be converted to Celcius for the operation, first.
  • Refactored code such that temperature types are preserved internally. Previously, everything was converted to double float internally.
  • Fixed speeling error on faranheit.

This update will be utilized for any additional sourcecode that utilizes these classes and replaces the original source in the OP.

Temperature conversion classes:

//
// temperature_types.h
// Version timestamp: 2-9-2020, 1:33 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# pragma once

# include <string>
# include <sstream>
# include <boost/variant.hpp>
# include <iostream>

# include "Conversion_Constants.h"

class celcius; class kelvin; class Temperature; class fahrenheit; class rankine;
typedef boost::variant<celcius, kelvin, fahrenheit, rankine, double> celcius_variant;
typedef boost::variant<celcius, kelvin, fahrenheit, rankine, double> kelvin_variant;
typedef boost::variant<celcius, kelvin, fahrenheit, rankine, double> fahrenheit_variant;
typedef boost::variant<celcius, kelvin, fahrenheit, rankine, double> rankine_variant;

class fahrenheit
{
    
public:
    double degrees_fahrenheit = 0;
    fahrenheit()
        : degrees_fahrenheit(0) {}
    fahrenheit(const fahrenheit& c) { degrees_fahrenheit = c.degrees_fahrenheit; } // Copy contructor
    fahrenheit(double r) { degrees_fahrenheit = r; }
    fahrenheit(fahrenheit_variant &r);
    void operator=(fahrenheit_variant &temp2);
    virtual fahrenheit operator-(fahrenheit_variant temp2);
    virtual fahrenheit operator+(fahrenheit_variant temp2); 
    virtual fahrenheit operator*(fahrenheit_variant temp2); 
    virtual fahrenheit operator/(fahrenheit_variant temp2); 
    virtual fahrenheit operator-=(fahrenheit_variant temp2);
    virtual fahrenheit operator+=(fahrenheit_variant temp2); 
    virtual fahrenheit operator++(int incr);
    virtual fahrenheit operator--(int incr); 
    virtual fahrenheit operator*=(fahrenheit_variant temp2);
    virtual fahrenheit operator/=(fahrenheit_variant temp2); 
    virtual double operator()(fahrenheit_variant temp2);
    virtual double operator()(); 
    virtual bool operator>(fahrenheit_variant temp2); 
    virtual bool operator<(fahrenheit_variant temp2); 
    virtual bool operator>=(fahrenheit_variant temp2); 
    virtual bool operator<=(fahrenheit_variant temp2); 
    virtual bool operator==(fahrenheit_variant temp2); 
    virtual bool operator!=(fahrenheit_variant temp2); 
    virtual operator celcius();
    virtual operator kelvin();
    virtual operator rankine();

    friend std::ostream& operator<<(std::ostream& lhs, const fahrenheit& rhs)
    {
        return (lhs << rhs.degrees_fahrenheit);
    }
    
    friend fahrenheit operator+(const double &c1, fahrenheit c2); 
    friend fahrenheit operator*(const double &c1, fahrenheit c2); 
    friend fahrenheit operator/(const double &c1, fahrenheit c2); 
    friend fahrenheit operator-(const double &c1, fahrenheit c2); 
    
    explicit operator double() const { return degrees_fahrenheit;}
    explicit operator float() const { return degrees_fahrenheit;}
    explicit operator int() const { return (int)(degrees_fahrenheit + 0.5);}
};


class kelvin 
{
    
public:
    double degrees_kelvin = 0;
    kelvin()
        : degrees_kelvin(0) {}
    kelvin(const kelvin& c) { degrees_kelvin = c.degrees_kelvin; } // Copy contructor
    kelvin(double r) { degrees_kelvin = r; }
    kelvin(kelvin_variant &r);
    void operator=(kelvin_variant &temp2);
    virtual kelvin operator-(kelvin_variant temp2);
    virtual kelvin operator+(kelvin_variant temp2); 
    virtual kelvin operator*(kelvin_variant temp2); 
    virtual kelvin operator/(kelvin_variant temp2); 
    virtual kelvin operator-=(kelvin_variant temp2);
    virtual kelvin operator+=(kelvin_variant temp2); 
    virtual kelvin operator++(int incr);
    virtual kelvin operator--(int incr); 
    virtual kelvin operator*=(kelvin_variant temp2);
    virtual kelvin operator/=(kelvin_variant temp2); 
    virtual double operator()(kelvin_variant temp2); 
    virtual double operator()(); 
    virtual bool operator>(kelvin_variant temp2); 
    virtual bool operator<(kelvin_variant temp2); 
    virtual bool operator>=(kelvin_variant temp2); 
    virtual bool operator<=(kelvin_variant temp2); 
    virtual bool operator==(kelvin_variant temp2); 
    virtual bool operator!=(kelvin_variant temp2); 
    virtual operator celcius();
    virtual operator fahrenheit();
    virtual operator rankine();

    friend std::ostream& operator<<(std::ostream& lhs, const kelvin& rhs)
    {
        return (lhs << rhs.degrees_kelvin);
    }
    
    friend kelvin operator+(const double &c1, kelvin c2); 
    friend kelvin operator*(const double &c1, kelvin c2); 
    friend kelvin operator/(const double &c1, kelvin c2); 
    friend kelvin operator-(const double &c1, kelvin c2); 
    
    explicit operator double() const { return degrees_kelvin;}
    explicit operator float() const { return degrees_kelvin;}
    explicit operator int() const { return (int)(degrees_kelvin + 0.5);}
};


class celcius
{
    
public:
    double degrees_celcius = 0;
    celcius()
        : degrees_celcius(0) {}
    celcius(const celcius& c) { degrees_celcius = c.degrees_celcius; } // Copy contructor
    celcius(double r) { degrees_celcius = r; }
    celcius(celcius_variant &r);
    void operator=(celcius_variant &temp2);
    virtual celcius operator-(celcius_variant temp2);
    virtual celcius operator+(celcius_variant temp2); 
    virtual celcius operator*(celcius_variant temp2); 
    virtual celcius operator/(celcius_variant temp2); 
    virtual celcius operator-=(celcius_variant temp2);
    virtual celcius operator+=(celcius_variant temp2); 
    virtual celcius operator++(int incr);
    virtual celcius operator--(int incr); 
    virtual celcius operator*=(celcius_variant temp2);
    virtual celcius operator/=(celcius_variant temp2); 
    virtual double operator()(celcius_variant temp2); 
    virtual double operator()(); 
    virtual bool operator>(celcius_variant temp2); 
    virtual bool operator<(celcius_variant temp2); 
    virtual bool operator>=(celcius_variant temp2); 
    virtual bool operator<=(celcius_variant temp2); 
    virtual bool operator==(celcius_variant temp2); 
    virtual bool operator!=(celcius_variant temp2); 
    virtual operator kelvin();
    virtual operator fahrenheit();
    virtual operator rankine();
    
    friend std::ostream& operator<<(std::ostream& lhs, const celcius& rhs)
    {
        return (lhs << rhs.degrees_celcius);
    }
    
    friend celcius operator+(const double &c1, const celcius &c2); 
    friend celcius operator*(const double &c1, const celcius &c2); 
    friend celcius operator/(const double &c1, const celcius &c2); 
    friend celcius operator-(const double &c1, const celcius &c2); 
    
    explicit operator double() const { return degrees_celcius;}
    explicit operator float() const { return degrees_celcius;}
    explicit operator int() const { return (int)(degrees_celcius + 0.5);}
    
};

class rankine
{
    
public:
    double degrees_rankine = 0;
    rankine()
        : degrees_rankine(0) {}
    rankine(const rankine& c) { degrees_rankine = c.degrees_rankine; } // Copy contructor
    rankine(double r) { degrees_rankine = r; }
    rankine(rankine_variant &r);
    void operator=(rankine_variant &temp2);
    virtual rankine operator-(rankine_variant temp2);
    virtual rankine operator+(rankine_variant temp2); 
    virtual rankine operator*(rankine_variant temp2); 
    virtual rankine operator/(rankine_variant temp2); 
    virtual rankine operator-=(rankine_variant temp2);
    virtual rankine operator+=(rankine_variant temp2); 
    virtual rankine operator++(int incr);
    virtual rankine operator--(int incr); 
    virtual rankine operator*=(rankine_variant temp2);
    virtual rankine operator/=(rankine_variant temp2); 
    virtual double operator()(rankine_variant temp2); 
    virtual double operator()(); 
    virtual bool operator>(rankine_variant temp2); 
    virtual bool operator<(rankine_variant temp2); 
    virtual bool operator>=(rankine_variant temp2); 
    virtual bool operator<=(rankine_variant temp2); 
    virtual bool operator==(rankine_variant temp2); 
    virtual bool operator!=(rankine_variant temp2); 
    virtual operator kelvin();
    virtual operator fahrenheit();
    virtual operator celcius();
    
    friend std::ostream& operator<<(std::ostream& lhs, const rankine& rhs)
    {
        return (lhs << rhs.degrees_rankine);
    }
    
    friend rankine operator+(const double &c1, const rankine &c2); 
    friend rankine operator*(const double &c1, const rankine &c2); 
    friend rankine operator/(const double &c1, const rankine &c2); 
    friend rankine operator-(const double &c1, const rankine &c2); 
    
    explicit operator double() const { return degrees_rankine;}
    explicit operator float() const { return degrees_rankine;}
    explicit operator int() const { return (int)(degrees_rankine + 0.5);}
};



class tcelcius_variant : public boost::static_visitor<celcius>
{
public:
    celcius operator()(double temperature) const
    {
        return (temperature);
    }
    
    celcius operator()(celcius temperature) const
    {
        return (temperature);
    }
    
    celcius operator()(kelvin temperature) const
    {
        celcius temp = temperature.degrees_kelvin - 273.15;
        return (temp);
    }
    
    celcius operator()(fahrenheit temperature) const
    {
        celcius temp = (temperature.degrees_fahrenheit - 32.0) / (1.8);
        return (temp);
    }
    
    celcius operator()(rankine temperature) const
    {
        celcius temp = (temperature.degrees_rankine -  491.67) / (1.8);
        return (temp);
    }
    
};

class tkelvin_variant : public boost::static_visitor<kelvin>
{
public:
    kelvin operator()(double temperature) const
    {
        return (temperature);
    }
    
    kelvin operator()(kelvin temperature) const
    {
        return (temperature);
    }
    
    kelvin operator()(celcius temperature) const
    {
        kelvin temp = temperature.degrees_celcius + 273.15;
        return (temp);
    }
    
    kelvin operator()(fahrenheit temperature) const
    {
        kelvin temp = (temperature.degrees_fahrenheit - 32.0) / (1.8) + 273.15;
        return (temp);
    }
    
    kelvin operator()(rankine temperature) const
    {
        kelvin temp = (temperature.degrees_rankine) / (1.8);
        return (temp);
    }
    
};

class tfahrenheit_variant : public boost::static_visitor<fahrenheit>
{
public:
    fahrenheit operator()(double temperature) const
    {
        return (temperature);
    }
    
    fahrenheit operator()(fahrenheit temperature) const
    {
        return (temperature);
    }
    
    fahrenheit operator()(celcius temperature) const
    {
        fahrenheit temp = temperature.degrees_celcius * (1.8) + 32.0;
        return (temp);
    }
    
    fahrenheit operator()(kelvin temperature) const
    {
        fahrenheit temp = (temperature.degrees_kelvin) * (1.8) - 459.67;
        return (temp);
    }
    
    fahrenheit operator()(rankine temperature) const
    {
        fahrenheit temp = (temperature.degrees_rankine) - 459.67;
        return (temp);
    }
    
};

class trankine_variant : public boost::static_visitor<rankine>
{
public:
    rankine operator()(double temperature) const
    {
        return (temperature);
    }
    
    rankine operator()(rankine temperature) const
    {
        return (temperature);
    }
    
    rankine operator()(celcius temperature) const
    {
        rankine temp = (temperature.degrees_celcius + 273.15) * (1.8);
        return (temp);
    }
    
    rankine operator()(kelvin temperature) const
    {
        rankine temp = (temperature.degrees_kelvin) * (1.8);
        return (temp);
    }
    
    rankine operator()(fahrenheit temperature) const
    {
        rankine temp = (temperature.degrees_fahrenheit) + 459.67;
        return (temp);
    }
    
};
//
// Conversion_Utilities.h
// Version timestamp: 2-9-2020, 7:50 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# pragma once
# include "temperature_types.h"
# include "power_types.h"
# include "volume_types.h"
# include "pressure_types.h"
# include "mass_types.h"
# include "volume_types.h"

# ifdef __cplusplus
extern "C" {
# endif

    int Conversion_UtilitiesTest();

# ifdef __cplusplus
}
# endif
//
// Kelvin.cpp
// Version timestamp: 2-9-2020, 1:33 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# include "temperature_types.h"

kelvin::kelvin(kelvin_variant &r)
{
    kelvin_variant c = r;
    kelvin temp = boost::apply_visitor(tkelvin_variant(), c);
    this->degrees_kelvin = temp.degrees_kelvin;
}

kelvin::operator fahrenheit()
{
    fahrenheit temp = (this->degrees_kelvin) * (1.8) - 459.67;
    return (temp);
}

kelvin::operator celcius()
{
    celcius temp = this->degrees_kelvin - 273.15;
    return (temp);
}

kelvin::operator rankine()
{
    rankine temp = this->degrees_kelvin * (1.8);
    return (temp);
}


void kelvin::operator=(kelvin_variant &temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    this->degrees_kelvin = temp.degrees_kelvin;
}

kelvin kelvin::operator-(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp -= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((kelvin)temp);
}

kelvin kelvin::operator+(celcius_variant temp2) 
{
    celcius temp = celcius(*this);
    temp += boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((kelvin)temp);
}

kelvin operator+(const double &c1, kelvin c2)
{
    celcius temp = c1 + celcius(c2);
    return ((kelvin)temp);
}

kelvin operator*(const double &c1, kelvin c2)
{
    celcius temp = c1 * celcius(c2);
    return ((kelvin)temp);
}

kelvin operator/(const double &c1, kelvin c2)
{
    celcius temp = c1 / celcius(c2);
    return ((kelvin)temp);
}

kelvin operator-(const double &c1, kelvin c2)
{
    celcius temp = c1 - celcius(c2);
    return ((kelvin)temp);
}

kelvin kelvin::operator*(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp *= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((kelvin)temp);
}

kelvin kelvin::operator/(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp /= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((kelvin)temp);
}

double kelvin::operator()(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    return (temp.degrees_kelvin);
}

double kelvin::operator()() 
{
    double temp = this->degrees_kelvin;
    return (temp);
}

kelvin kelvin::operator-=(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp - boost::apply_visitor(tcelcius_variant(), temp2);
    kelvin tempk = temp;
    this->degrees_kelvin = tempk.degrees_kelvin;
    
    return (tempk);
}

kelvin kelvin::operator+=(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp + boost::apply_visitor(tcelcius_variant(), temp2);
    kelvin tempk = temp;
    this->degrees_kelvin = tempk.degrees_kelvin;
    
    return (tempk);
}

kelvin kelvin::operator++(int incr) 
{
    this->degrees_kelvin +=  1.0;
    return (this->degrees_kelvin);
}

kelvin kelvin::operator--(int incr) 
{
    this->degrees_kelvin -= 1.0;
    return (this->degrees_kelvin);
}

kelvin kelvin::operator*=(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp * boost::apply_visitor(tcelcius_variant(), temp2);
    kelvin tempk = temp;
    this->degrees_kelvin = tempk.degrees_kelvin;
    
    return (tempk);
}

kelvin kelvin::operator/=(kelvin_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp / boost::apply_visitor(tcelcius_variant(), temp2);
    kelvin tempk = temp;
    this->degrees_kelvin = tempk.degrees_kelvin;
    
    return (tempk);
}

bool kelvin::operator>(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (this->degrees_kelvin > temp.degrees_kelvin) return (1);
    return (0);
}

bool kelvin::operator<(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (this->degrees_kelvin < temp.degrees_kelvin) return (1);
    return (0);
}

bool kelvin::operator==(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (fabs(temp.degrees_kelvin - this->degrees_kelvin) <= 
    __DBL_EPSILON__ * fmax(fabs(temp.degrees_kelvin), fabs(this->degrees_kelvin))) return (1);
    return (0);
}

bool kelvin::operator!=(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (fabs(temp.degrees_kelvin - this->degrees_kelvin) <= 
        __DBL_EPSILON__ * fmax(fabs(temp.degrees_kelvin), fabs(this->degrees_kelvin))) return (0);
    return (1);
}

bool kelvin::operator>=(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (this->degrees_kelvin >= temp.degrees_kelvin) return (1);
    return (0);
}
bool kelvin::operator<=(kelvin_variant temp2) 
{
    kelvin temp = boost::apply_visitor(tkelvin_variant(), temp2);
    if (this->degrees_kelvin <= temp.degrees_kelvin) return (1);
    return (0);
}
//
// fahrenheit.cpp
// Version timestamp: 2-9-2020, 1:33 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# include "temperature_types.h"

fahrenheit::fahrenheit(fahrenheit_variant &r)
{
    fahrenheit_variant c = r;
    
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), c);
    this->degrees_fahrenheit = temp.degrees_fahrenheit;
}

fahrenheit::operator kelvin()
{
    kelvin temp = (this->degrees_fahrenheit - 32.0) / (1.8) + 273.15;
    return (temp);
}

fahrenheit::operator celcius()
{
    celcius temp = (this->degrees_fahrenheit - 32.0) / (1.8);
    return (temp);
}

fahrenheit::operator rankine()
{
    rankine temp = (this->degrees_fahrenheit + 459.67);
    return (temp);
}

void fahrenheit::operator=(fahrenheit_variant &temp2) 
{
    fahrenheit temp = boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_fahrenheit = temp.degrees_fahrenheit;
}

fahrenheit fahrenheit::operator-(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp -= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((fahrenheit)temp);
}

fahrenheit fahrenheit::operator+(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp += boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((fahrenheit)temp);
}

fahrenheit operator+(const double &c1, fahrenheit c2)
{
    celcius temp = c1 + celcius(c2);
    return ((fahrenheit)temp);
}

fahrenheit operator*(const double &c1, fahrenheit c2)
{
    celcius temp = c1 * celcius(c2);
    return ((fahrenheit)temp);
}

fahrenheit operator/(const double &c1, fahrenheit c2)
{
    celcius temp = c1 / celcius(c2);
    return ((fahrenheit)temp);
}

fahrenheit operator-(const double &c1, fahrenheit c2)
{
    celcius temp = c1 - celcius(c2);
    return ((fahrenheit)temp);
}

fahrenheit fahrenheit::operator*(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp *= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((fahrenheit)temp);
}

fahrenheit fahrenheit::operator/(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp /= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((fahrenheit)temp);
}

double fahrenheit::operator()(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    return (temp.degrees_fahrenheit);
}

double fahrenheit::operator()() 
{
    double temp = this->degrees_fahrenheit;
    return (temp);
}

fahrenheit fahrenheit::operator-=(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp - boost::apply_visitor(tcelcius_variant(), temp2);
    fahrenheit tempf = temp;
    this->degrees_fahrenheit = tempf.degrees_fahrenheit;
    
    return (tempf);
}

fahrenheit fahrenheit::operator+=(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp + boost::apply_visitor(tcelcius_variant(), temp2);
    fahrenheit tempf = temp;
    this->degrees_fahrenheit = tempf.degrees_fahrenheit;
    
    return (tempf);
}

fahrenheit fahrenheit::operator++(int incr) 
{
    this->degrees_fahrenheit +=  1.0;
    return (this->degrees_fahrenheit);
}

fahrenheit fahrenheit::operator--(int incr) 
{
    this->degrees_fahrenheit -=  1.0;
    return (this->degrees_fahrenheit);
}

fahrenheit fahrenheit::operator*=(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp * boost::apply_visitor(tcelcius_variant(), temp2);
    fahrenheit tempf = temp;
    this->degrees_fahrenheit = tempf.degrees_fahrenheit;
    
    return (tempf);
}

fahrenheit fahrenheit::operator/=(fahrenheit_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp / boost::apply_visitor(tcelcius_variant(), temp2);
    fahrenheit tempf = temp;
    this->degrees_fahrenheit = tempf.degrees_fahrenheit;
    
    return (tempf);
}

bool fahrenheit::operator>(fahrenheit_variant temp2) 
{ 
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (this->degrees_fahrenheit > temp.degrees_fahrenheit) return (1);
    return (0);
}

bool fahrenheit::operator<(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (this->degrees_fahrenheit < temp.degrees_fahrenheit) return (1);
    return (0);
}

bool fahrenheit::operator==(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (fabs(temp.degrees_fahrenheit - this->degrees_fahrenheit) <= 
    __DBL_EPSILON__ * fmax(fabs(temp.degrees_fahrenheit), fabs(this->degrees_fahrenheit))) return (1);
    return (0);
}

bool fahrenheit::operator!=(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (fabs(temp.degrees_fahrenheit - this->degrees_fahrenheit) <= 
    __DBL_EPSILON__ * fmax(fabs(temp.degrees_fahrenheit), fabs(this->degrees_fahrenheit))) return (0);
    return (1);
}

bool fahrenheit::operator>=(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (this->degrees_fahrenheit >= temp.degrees_fahrenheit) return (1);
    return (0);
}
bool fahrenheit::operator<=(fahrenheit_variant temp2) 
{
    fahrenheit temp = boost::apply_visitor(tfahrenheit_variant(), temp2);
    if (this->degrees_fahrenheit <= temp.degrees_fahrenheit) return (1);
    return (0);
}
//
// Celcius.cpp
// Version timestamp: 2-9-2020, 1:33 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# include "temperature_types.h"

celcius::celcius(celcius_variant &r)
{
    celcius_variant c = r;
    
    celcius temp = boost::apply_visitor(tkelvin_variant(), c);
    this->degrees_celcius = temp.degrees_celcius;
}

celcius::operator kelvin()
{
    kelvin temp = this->degrees_celcius + 273.15;
    return (temp);
}

celcius::operator fahrenheit()
{
    fahrenheit temp = this->degrees_celcius * (1.8) + 32.0;
    return (temp);
}

celcius::operator rankine()
{
    rankine temp = (this->degrees_celcius + 273.15) * (1.8);
    return (temp);
}

void celcius::operator=(celcius_variant &temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_celcius = temp.degrees_celcius;
}

celcius celcius::operator-(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius - boost::apply_visitor(tcelcius_variant(), temp2);
    return (temp);
}

celcius celcius::operator+(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius + boost::apply_visitor(tcelcius_variant(), temp2);
    return (temp);
}

celcius operator+(const double &c1, const celcius &c2)
{
    celcius temp = c1 + c2.degrees_celcius;
    return (temp);
}

celcius operator*(const double &c1, const celcius &c2)
{
    celcius temp = c1 * c2.degrees_celcius;
    return (temp);
}

celcius operator/(const double &c1, const celcius &c2)
{
    celcius temp = c1 / c2.degrees_celcius;
    return (temp);
}

celcius operator-(const double &c1, const celcius &c2)
{
    celcius temp = c1 - c2.degrees_celcius;
    return (temp);
}

celcius celcius::operator++(int incr) 
{
    this->degrees_celcius +=  1.0;
    return (this->degrees_celcius);
}

celcius celcius::operator--(int incr) 
{
    this->degrees_celcius -=  1.0;
    return (this->degrees_celcius);
}

celcius celcius::operator*(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius * boost::apply_visitor(tcelcius_variant(), temp2);
    return (temp);
}

celcius celcius::operator/(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius / boost::apply_visitor(tcelcius_variant(), temp2);
    return (temp);
}

double celcius::operator()(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    return (temp.degrees_celcius);
}

double celcius::operator()() 
{
    celcius temp = this->degrees_celcius;
    return (temp.degrees_celcius);
}

celcius celcius::operator-=(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius - boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_celcius = temp.degrees_celcius;
    return (this->degrees_celcius);
}

celcius celcius::operator+=(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius + boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_celcius = temp.degrees_celcius;
    return (this->degrees_celcius);
}

celcius celcius::operator*=(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius * boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_celcius = temp.degrees_celcius;
    return (this->degrees_celcius);
}

celcius celcius::operator/=(celcius_variant temp2) 
{
    celcius temp = this->degrees_celcius / boost::apply_visitor(tcelcius_variant(), temp2);
    this->degrees_celcius = temp.degrees_celcius;
    return (this->degrees_celcius);
}

bool celcius::operator>(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (this->degrees_celcius > temp.degrees_celcius) return (1);
    return (0);
}

bool celcius::operator<(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (this->degrees_celcius < temp.degrees_celcius) return (1);
    return (0);
}

bool celcius::operator==(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (fabs(temp.degrees_celcius - this->degrees_celcius) <= 
        __DBL_EPSILON__ * fmax(fabs(temp.degrees_celcius), fabs(this->degrees_celcius))) return (1);
    return (0);
}

bool celcius::operator!=(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (fabs(temp.degrees_celcius - this->degrees_celcius) <=
        __DBL_EPSILON__ * fmax(fabs(temp.degrees_celcius), fabs(this->degrees_celcius))) return (0);
    return (1);
}

bool celcius::operator>=(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (this->degrees_celcius >= temp.degrees_celcius) return (1);
    return (0);
}

bool celcius::operator<=(celcius_variant temp2) 
{
    celcius temp = boost::apply_visitor(tcelcius_variant(), temp2);
    if (this->degrees_celcius <= temp.degrees_celcius) return (1);
    return (0);
}

CC BY-SA 4.0

3 Likes
//
// Rankine.cpp
// Version timestamp: 2-9-2020, 1:33 PM
//
// Attribution : Copyright (c) 2020 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# include "temperature_types.h"

rankine::rankine(rankine_variant &r)
{
    rankine_variant c = r;
    rankine temp = boost::apply_visitor(trankine_variant(), c);
    this->degrees_rankine = temp.degrees_rankine;
}

rankine::operator fahrenheit()
{
    fahrenheit temp = (this->degrees_rankine) - 459.67;
    return (temp);
}

rankine::operator celcius()
{
    celcius temp = (this->degrees_rankine - 491.67) / (1.8);
    return (temp);
}

rankine::operator kelvin()
{
    kelvin temp = this->degrees_rankine * (1.8);
    return (temp);
}

void rankine::operator=(rankine_variant &temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    this->degrees_rankine = temp.degrees_rankine;
}

rankine rankine::operator-(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp -= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((rankine)temp);
}

rankine rankine::operator+(celcius_variant temp2) 
{
    celcius temp = celcius(*this);
    temp += boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((rankine)temp);
}

rankine operator+(const double &c1, rankine c2)
{
    celcius temp = c1 + celcius(c2);
    return ((rankine)temp);
}

rankine operator*(const double &c1, rankine c2)
{
    celcius temp = c1 * celcius(c2);
    return ((rankine)temp);
}

rankine operator/(const double &c1, rankine c2)
{
    celcius temp = c1 / celcius(c2);
    return ((rankine)temp);
}

rankine operator-(const double &c1, rankine c2)
{
    celcius temp = c1 - celcius(c2);
    return ((rankine)temp);
}

rankine rankine::operator*(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp *= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((rankine)temp);
}

rankine rankine::operator/(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp /= boost::apply_visitor(tcelcius_variant(), temp2);
    
    return ((rankine)temp);
}

double rankine::operator()(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    return (temp.degrees_rankine);
}

double rankine::operator()() 
{
    double temp = this->degrees_rankine;
    return (temp);
}

rankine rankine::operator-=(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp - boost::apply_visitor(tcelcius_variant(), temp2);
    rankine tempk = temp;
    this->degrees_rankine = tempk.degrees_rankine;
    
    return (tempk);
}

rankine rankine::operator+=(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp + boost::apply_visitor(tcelcius_variant(), temp2);
    rankine tempk = temp;
    this->degrees_rankine = tempk.degrees_rankine;
    
    return (tempk);
}

rankine rankine::operator++(int incr) 
{
    this->degrees_rankine +=  1.0;
    return (this->degrees_rankine);
}

rankine rankine::operator--(int incr) 
{
    this->degrees_rankine -= 1.0;
    return (this->degrees_rankine);
}

rankine rankine::operator*=(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp * boost::apply_visitor(tcelcius_variant(), temp2);
    rankine tempk = temp;
    this->degrees_rankine = tempk.degrees_rankine;
    
    return (tempk);
}

rankine rankine::operator/=(rankine_variant temp2) 
{
    celcius temp = celcius(*this);
    temp = temp / boost::apply_visitor(tcelcius_variant(), temp2);
    rankine tempk = temp;
    this->degrees_rankine = tempk.degrees_rankine;
    
    return (tempk);
}

bool rankine::operator>(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (this->degrees_rankine > temp.degrees_rankine) return (1);
    return (0);
}

bool rankine::operator<(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (this->degrees_rankine < temp.degrees_rankine) return (1);
    return (0);
}

bool rankine::operator==(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (fabs(temp.degrees_rankine - this->degrees_rankine) <= 
    __DBL_EPSILON__ * fmax(fabs(temp.degrees_rankine), fabs(this->degrees_rankine))) return (1);
    return (0);
}

bool rankine::operator!=(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (fabs(temp.degrees_rankine - this->degrees_rankine) <= 
        __DBL_EPSILON__ * fmax(fabs(temp.degrees_rankine), fabs(this->degrees_rankine))) return (0);
    return (1);
}

bool rankine::operator>=(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (this->degrees_rankine >= temp.degrees_rankine) return (1);
    return (0);
}
bool rankine::operator<=(rankine_variant temp2) 
{
    rankine temp = boost::apply_visitor(trankine_variant(), temp2);
    if (this->degrees_rankine <= temp.degrees_rankine) return (1);
    return (0);
}

Vapor Pressure Deficit classes:

See earlier posts with the VPD class.

Definitions :

//
// Constants.h
// Version timestamp: 9-26-2018, 10:45 PM
//
// Attribution : Copyright (c) 2018 Northern_Loki (sha256::6F290BF833967127BE26C92C8F6B1C1A3949C55A7EABCEF3ECC785CD2D38D30D)
// License is granted under the Creative Commons Attribution-ShareAlike 4.0 International.  https://creativecommons.org/licenses/by-sa/4.0/
//
# pragma once

/* Euler's constant */
# define EULER 2.7182818284590452353602875

/* Avogadro's Constant SI unit (1/mol) */
# define NA (6.022140758 * pow(10,23))

/* Boltzmann's constant (J/K) */
# define BOLTZMANN (1.38064852 * (pow(10,āˆ’23)))


/* Stefan-Boltzmann's constant SI units (W/(m^2.K^4)) */
# define STEFAN_BOLTZMANN (5.67036713 * (pow(10,-8)))

/* Latent heat of vaporization 1ATM and 0 degrees C SI units (J/g) */
# define L0 2500

/* Latent heat of vaporization 1ATM and 20 degrees C SI units (J/g) */
# define L20 2450

/* Latent heat of vaporization 1ATM and 100 degrees C SI units (J/g) */
# define L100 2260

/* Density of dry air 1ATM and 0 degrees C SI units (kg/m^3) */
# define p_air0 1.2922

/* Isobaric heat capacity of dry air 1ATM and 0 degrees C SI units (J/kg K) */
# define Cp_air0 1010

/* Molecular weight of water (g/mol) */
# define M_H20 18.0


/**************/
/* Conversion */
/**************/
# define TO_DEGREE(radians) ((radians) * 57.295779513082320876798154814105)
# define TO_RADIAN(degrees) ((degrees) * 0.017453292519943295769236907684886)


/*********/
/* Time  */
/********/
/* Julian J2000 */
# define J2000 (2451545.0)

/* Julian UNIX epoch 0h Jan 1, 1970 */
# define UNIX_JULIAN_EPOCH (2440587.5)

/* Julian days per year */
# define JULIAN_DAYS_YEAR (365.25)

/* Julian days per century */
# define JULIAN_DAYS_CENTURY  (JULIAN_DAYS_YEAR) * 100.0

/* Solar days per century */
# define SOLAR_CENTURY (36524.22)

/* Hours per day*/
# define HOURS_DAY (24.0)

/* Minutes per hour */
# define MINUTES_HOUR (60.0)

/* Minutes per day */
# define MINUTES_DAY (MINUTES_HOUR) * (HOURS_DAY)

/* Seconds per minute*/
# define SECONDS_MINUTE (60.0)

/* Seconds per hour (3600) */
# define SECONDS_HOUR ((SECONDS_MINUTE) * (MINUTES_HOUR))

/* Seconds per day (86400) */
# define SECONDS_DAY ((HOURS_DAY) * (SECONDS_HOUR))

/* Mean tropical days per year */
# define MEAN_TROPICAL_DAYS_YEAR (365.242189)

/* Mean sidereal days per year */
# define SIDEREAL_DAYS_YEAR (365.25636)


/**************/
/* Distance   */
/**************/

/* Astronomical units. Kilometers per AU */ 
# define AU_KM (149597870.700) 


/**************/
/*   Solar   */
/*************/

/* Solar constant W/m^2 (averaged of a year) Page, 1986 */
# define SOLAR_CONSTANT 1366.9444

/* Planetary Atmospheric Albedo Mean */
# define ALBEDO_MEAN_Rp 0.3

/* Sun Blackbody Temperature in Kelvin */
# define SUN_BLACKBODY_TEMP 5777

/**************/
/* Pressure   */
/*************/

/* Average atmospheric pressure at sea level (Pascals) */
# define ATM1_AVE 101325.0
	
/* Pascals to millibars */
# define TO_MILLIBAR(pascal) (pascal / 100.0)
	
/* Millibars to pascals */
# define TO_PASCAL(millibars) (millibars * 100.0)

CC BY-SA 4.0

3 Likes

@Northern_Loki Do you have a git project or anything for this code? or you just post the sources here as and when you make any changes?

1 Like

A repository can be found over here: github link

I generally utilize a local version control system as part of a larger project. What is on github is not really a project of any sort, currently only updated occasionally, placed there for convenience, and it consists of the source posted on OG. Havenā€™t the time or inclination to build a thorough github ā€œexperienceā€ at this point. Barely have the time to work on the source :slight_smile:

.

5 Likes

Lokiā€™s is much more sophisticated and useful; but for anyone who is scared with compiled programs and likes the web I transposed his to plain jane JavaScript. Prints out the tables and nothing more. I used the buck 1981.

Feel free to modify and play with it anyway you like =)

5 Likes

I was hoping someone would contribute some additional options beyond the compiled c/c++. Excited. :+1:

Thanks @plant! Itā€™s a much more a compact, efficient, and portable implementation. Very nice work.

8 Likes

oh i so wish i found this forum 6 months beforeā€¦ nice job mateā€¦

7 Likes

Iā€™m having a matrix neo momentā€¦ waking in the chair, ā€œI know C++ā€. Show meā€¦
My hat off to you, outstanding work.

4 Likes

Awesome work @Northern_Loki.

Tagged for the trip down the rabbit holeā€¦ :call_me_hand:

1 Like