Skip to content

Conversation

cbmarini
Copy link
Collaborator

@cbmarini cbmarini commented Oct 7, 2025

This PR introduces a new StrictRounding statement that rounds floats to a specified precision. If no precision is given, the default precision is used. This can be useful to trim the extra precision used by GMP. This also resolves #698.

Similar to the floating point precision, the precision in StrictRounding can be specified in digits or bits. For example:

  • the base 10 number 1.1e-4 cannot be written as a finite sequence in base 2: it is 1.1100110101011111101*2^(-14). So, when we round to 5 bits, we get 1.1101*2^(-14) in bits. However, in base 10, this is written as 1.10626220703125e-04.

At the moment, the implementation makes use of the GMP functions mpf_get_str and mpf_set_str. Maybe in the feature we can come up with our own algorithm to do the rounding?

Example usage:

#-
Off statistics;
#StartFloat 6d
CFunction f;
Local F = f(1.0)+f(1.0000001);
L G = 1.23456789e-4;
Print;
.sort

StrictRounding 4d;
Argument f;
	StrictRounding;
EndArgument;
Print;
.end

results in

   F =
      f(1.0e+00) + f(1.0e+00);

   G =
      1.23457e-04;


   F =
      2*f(1.0e+00);

   G =
      1.235e-04;

@coveralls
Copy link

coveralls commented Oct 7, 2025

Coverage Status

coverage: 56.79% (+0.01%) from 56.779%
when pulling 9088a07 on cbmarini:strict-rounding
into 2aba64b on form-dev:master.

… GMP precision

- The user can specify a precision (in bits or digits) for the rounding. If no number is given, the default precision is used.
- Ensures that extra internal GMP precision (used for tracking accuracy) is cut off and properly rounded
- Internally the mpf_get_str and mpf_set_str are used. This means we could also implement base 2 rounding.
- refactor: changed AO.floatsize such that it can now also hold floats in base 2.
- This resolves form-dev#698
- added a test and  documentation for strictrounding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comparing float arguments with limited precision

2 participants