I defined a struct:
struct Low {
double high;
double low;
double fraction; // = (Low.high - Low.low) / Low.high;
double percentage; // = Low.fraction * 100;
} low2015, low2018, low2021, low2022;
The comments hint at what I am getting at.
I would like to introduce an expression in the definition block of the struct Low
so that I do not have to manually do the assignment of my_struct.fraction
and my_struct.percentage
low2021.high = 64854;
low2021.low = 28805;
low2021.fraction = (low2021.high - low2021.low) / low2021.high;
low2021.percentage = low2021.fraction * 100;
for all the four variables, i.e. low2015
, low2018
, low2021
, low2022
.
How might I accomplish this?
CodePudding user response:
The structure is small enough to be returned by value. Consider adding a function MakeLow()
.
struct Low {
double high;
double low;
double fraction;
double percentage;
};
static inline struct Low MakeLow(double high, double low) {
return (struct Low) {
.high = high,
.low = low,
.fraction = (high - low) / high,
.percentage = (high - low) / high * 100,
};
}
Usage:
struct Low low2021 = MakeLow(64854, 28805);