Login Page - Create Account

Support Board


Date/Time: Sat, 04 Jan 2025 07:46:40 +0000



Post From: Rounding floats in point and figure code

[2016-05-18 02:27:43]
@sstfrederik - Posts: 404
The code in studies8.cpp on PointAndFigureAddBoxes does not seem to be right.

if there is a float value of say: 1.13770 which in longer format can actually be 1.1376996223 This would mean it touched the 1.13770 value, but the below code will not round the float to that value since Epsilon is not correct. floor(1.13769996223 / 0.0001 + 0.5 * 0.00001) = 11376
We would like it to be adding 0.5 to the Value / Boxsize in order to round it properly.

I think epsilon should be 50000 * GetFormatStepValue(ValueFormat);

Did I miss anything?


double PointAndFigureAddBoxes(SCStudyInterfaceRef sc, double Value, double BoxSize, int Direction, unsigned long ValueFormat)
{
  double Epsilon = 0.5 * GetFormatStepValue(ValueFormat);

  if (Direction == 1) //up
  {
    int NumberOfBoxes = (int)floor(Value / BoxSize + Epsilon);
    return (NumberOfBoxes * BoxSize);
  }
  else if (Direction == 2) //down
  {
    int NumberOfBoxes = (int)ceil(Value / BoxSize - Epsilon);
    return (NumberOfBoxes * BoxSize);
  }
  else //unknown
    return sc.Round((float)(Value / BoxSize)) * BoxSize;

  return 0;
}