Skip to content

Relative Roundoff is Too Large #756

@tjlaboss

Description

@tjlaboss

Bug Description

When writing new floating point numbers, MontePy reverse-engineers the formatting string in order to preserve formatting. It does not appropriately consider how much of a relative change this is.

To Reproduce

import montepy
surf = montepy.Surface("1 SO 3.0")
surf._surface_constants[0].value = 1.249
print(surf.format_for_mcnp_input((6,3,0))[0])

Results in

1 SO 1.2

Describe the solution you'd like

MontePy should consider the relative difference that the user is willing to tolerate. There should be a small default number; eps=1e-5 is probably too large. MontePy should check whether the rounded number varies from the true number by more than eps. If so, it should find the maximum amount of rounding that will satisfy the threshold (i.e., determine the number of digits such that true*(1 - eps) >= rounded >= true*(1 + eps).

Describe alternatives you've considered

Alternatively, write the unrounded true number raising a LineExpansionWarning.

There are also a few ways to make the threshold user-configurable:

  1. Global constant that the user can change.
  2. Argument to all write() and format() functions
  3. Setting in Formatter class: see Idea: Formatter class #103

I think option 1 should be implemented immediately as a stopgap and option 3 long-term.

Another option could be an absolute threshold.

Version

  • I suspect 0.2.0 but haven't verified

Additional context

This is a critical issue.

Metadata

Metadata

Assignees

Labels

bugsA deviation from expected behavior that does not reach the level of being reportable as an "Error".criticalAn issue that seriously limits user adoption or hampers current use.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions