Skip to content

Fix float printing #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix float printing #676

wants to merge 2 commits into from

Conversation

cbmarini
Copy link
Collaborator

This fixes the float printing issue

#StartFloat 50
Local F = 1/3;
ToFloat;
Print;
.end

that resulted in

   F =
      3.3e-01;

The correct result is now

   F =
      3.333333333333333e-01;

Furthermore set the default MZV weight MAXWEIGHT to 0 instead of 40.

@cbmarini cbmarini requested a review from jodavies June 20, 2025 14:00
@jodavies
Copy link
Collaborator

Does SetupMZVTables need the same change? In that function there is some weight dependence on the precision that is used; I wonder if it is not easier to just allocate everything at the same, maximally-required precision? Then the logic to set the precision can all move to DoStartFloat and the multiple calls to SetFloatPrecision could all be removed?

@vermaseren
Copy link
Collaborator

vermaseren commented Jun 20, 2025 via email

@jodavies
Copy link
Collaborator

That sounds reasonable. Also a way to specify precision in digits rather than bits?

#startfloat 50b,MZV=0
#startfloat 50d,MZV=0

for example. Or do b and d look too similar there?

I also wonder: why can the regular mpf_init function not be used to set up the aux floats? Then everything should "just work" at the precision that is requested?

@cbmarini
Copy link
Collaborator Author

@jodavies, this can indeed use some cleaning up, see the following example:

#StartFloat 64,2
L F = mzv_(2);
Evaluate;
Print;
.end
   F =
      1.64493406684822643645e+00;

vs

#StartFloat 67,2
L F = mzv_(2);
Evaluate;
Print;
.end
   F =
      1.64493406684822643647e+00;

The last digit changed if we added a few more bits of precision.
Let me see if changing SetupMZVTables similarly resolves this problem.

@vermaseren, I also like the idea to be specific that the weights are for the MZVs. Maybe indeed also go with @jodavies suggestion to specify the precision in either bits or digits.

@vermaseren
Copy link
Collaborator

vermaseren commented Jun 20, 2025 via email

@cbmarini
Copy link
Collaborator Author

I see that the tests that printed some of the constants like pi_ now also need to be updated.

@cbmarini
Copy link
Collaborator Author

I changed SetupMPFTables such that it uses GMP's mpf_init. At the moment, the MZV tables are set with variable precision (see the comment at the beginning of SetupMZVTables). Do we still want this, or is it better to set the tables also with the highest precision using mpf_init?

@coveralls
Copy link

coveralls commented Jun 24, 2025

Coverage Status

coverage: 50.848% (+0.2%) from 50.603%
when pulling e622d40 on cbmarini:PrintFloat
into 21f29a8 on form-dev:master.

assert result("PI6") =~ expr("1.7724538509055160273e+00")
assert result("EE1") =~ expr("2.7182818284590452354e+00")
assert result("EE2") =~ expr("8.5397342226735670654e+00")
assert result("EM1") =~ expr("5.772156649015328606e-01")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense that there is a digit less here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean for EM1 specific or for all of them?

Copy link
Collaborator

@jodavies jodavies Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For EM1 specifically: the rest all have the same number of digits. The next would be a 1, from rounding 07, or? Does the conversion truncate rather than round (and then FORM cuts the trailing 0)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the next digits, one has 5.77215664901532860606e-01. So it looks like it got truncated: 60606 becomes 6060 by gmp_snprintf in PrintFloat. Then the parsing of FORM drops the last 0. So in total, one gets one digit less compared to the other examples in the test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more observation: running this test with one more bit (so #StartFloat 65), EE1 and EM1 change (the rest is unchanged), that is

   EE1 =
      2.7182818284590452353e+00;
   EM1 =
      5.7721566490153286061e-01;

The rounding for EM1 seems now more logical to me, whereas for EE1 the previous rounding (with 64 bits) seemed more logical.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think this is the expected behaviour of MPFR_RNDN.

@cbmarini cbmarini mentioned this pull request Jun 25, 2025
refactor: use GMP's mpf_init etc. to set up the aux floats and MZVTables.

test: updated the evaluate_symbol tests according to the fixed float printing.
- Removed double SetFloatPrecision call. Now only called once from DoStartFloat.
- Fixed precision handling in SimpleDelta and SimpleDeltaC.
- Adjusted printed digits to avoid overprinting from gmp_snprintf.

test: adjusted the test evaluate_symbol accordingly and added two tests for the evaluation of MZV's.
@cbmarini
Copy link
Collaborator Author

For the new test that evaluates all MZVs up to weight 6, we could consider increasing the weight. On my laptop, evaluating up to weight 10 only takes about 0.25 seconds.

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.

4 participants