I'm trying to write a program calculates various things for a train company. Two of the things I'm trying to calculate are amount of ore in a mine and amount of ore at a warehouse. The following code represents the calculations for each:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
if(Hour == 1)
{
MineOre = OreX + DigRate;
}
else if(Hour > 1)
{
MineOre = MineOre + DigRate;
if(Hour % 17 == 0)
{
if(MineOre >= OreMax)
{
MineOre = MineOre - OreMax;
}
else if(MineOre < OreMax)
{
MineOre = DigRate;
}
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
if(Hour == 1)
{
WarehouseOre = OreY;
}
if(Hour > 1)
{
if(WarehouseOre >= SellMax)
{
WarehouseOre = WarehouseOre - SellMax;
}
else if(WarehouseOre < SellMax)
{
WarehouseOre = 0.0;
}
}
if(Hour == 3)
{
WarehouseOre = WarehouseOre + OreMax;
}
if((Hour - 3) % 17 == 0)
{
WarehouseOre = WarehouseOre + TrainOre;
}
|
The code is in a while loop and information is being read in from a file. It takes the train 3 hours to get to the warehouse, hence the if statement at the end of the warehouse ore calculations. The modulus is so that the calculation only happens every 17 hours(inputs). This program is working fine until it gets about halfway through the file. (the current file being read has 249 hours (inputs)).
At the point the error occurs, the previous value for MineOre is 139.6, the OreMax is 140.0 and the DigRate value is 9.0, so the MineOre should equal 9.0, but it equals 8.60001. Also, the WarehouseOre value is 15 and the SellMax value is 10.8, so the new WarehouseOre value should be 4.2, but it is 4.199999. I know it is a precision error, but I do not know how to fix it since I'm not going to have an output for all 249 inputs, just one at the end, right now I'm simply checking it to make sure it's working (have more to do still). I know setprecision would work, but my understanding is that that is only for outputting a value. Any ideas?
OH! All numbers are float except for Hour, which is an int.