Skip to content

Commit

Permalink
fix: regression recognition of very small unit exponent as unitless
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreminger committed Jan 12, 2025
1 parent 67f7ba4 commit 77dcdbc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,8 @@ def ensure_dims_all_compatible(*args):
if len(args) == 1:
return first_arg

first_arg_dims = normalize_dims_dict(custom_get_dimensional_dependencies(first_arg))
if all(normalize_dims_dict(custom_get_dimensional_dependencies(arg)) == first_arg_dims for arg in args[1:]):
first_arg_dims = custom_get_dimensional_dependencies(first_arg)
if all(custom_get_dimensional_dependencies(arg) == first_arg_dims for arg in args[1:]):
return first_arg

raise TypeError('All input arguments to function need to have compatible units')
Expand Down Expand Up @@ -1726,7 +1726,7 @@ def get_dimensional_analysis_expression(parameter_subs: dict[Symbol, Expr],
def custom_get_dimensional_dependencies(expression: Expr | None):
if expression is not None:
expression = subs_wrapper(expression, {cast(Symbol, symbol): S.One for symbol in (expression.free_symbols - dimension_symbols)})
return dimsys_SI.get_dimensional_dependencies(expression)
return normalize_dims_dict(dimsys_SI.get_dimensional_dependencies(expression))

def dimensional_analysis(dimensional_analysis_expression: Expr | None, dim_sub_error: Exception | None,
custom_base_units: CustomBaseUnits | None = None):
Expand Down
17 changes: 15 additions & 2 deletions tests/test_basic.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ test('Test floating point exponent rounding', async () => {
await page.click('#add-math-cell');
await page.setLatex(5, String.raw`\mathrm{sum}\left(1\left\lbrack K\cdot s^{.000000000001}\right\rbrack,4\left\lbrack K\right\rbrack\right)=`);

// check small exponent rounding for dimensionless exponent check
await page.click('#add-math-cell');
await page.setLatex(6, String.raw`6^{1\left\lbrack s^{.0000000000001}\right\rbrack}=`);
await page.click('#add-math-cell');
await page.setLatex(7, String.raw`6^{1\left\lbrack s^{.000000000001}\right\rbrack}=`);

await page.waitForSelector('text=Updating...', {state: 'detached'});

let content = await page.textContent('#result-value-0');
Expand All @@ -808,7 +814,7 @@ test('Test floating point exponent rounding', async () => {
content = await page.textContent('#result-units-1');
expect(content).toBe('kg');

await expect(page.locator('#cell-2 >> text=Dimension Error')).toBeVisible();
await expect(page.locator('#cell-2 >> text=Dimension Error: Only equivalent dimensions can be added or subtracted')).toBeVisible();

content = await page.textContent('#result-value-3');
expect(parseLatexFloat(content)).toBeCloseTo(4, precision);
Expand All @@ -820,7 +826,14 @@ test('Test floating point exponent rounding', async () => {
content = await page.textContent('#result-units-4');
expect(content).toBe('K');

await expect(page.locator('#cell-5 >> text=Dimension Error')).toBeVisible();
await expect(page.locator('#cell-5 >> text=Dimension Error: All input arguments to function need to have compatible units')).toBeVisible();

content = await page.textContent('#result-value-6');
expect(parseLatexFloat(content)).toBeCloseTo(6, precision);
content = await page.textContent('#result-units-6');
expect(content).toBe('');

await expect(page.locator('#cell-7 >> text=Dimension Error: Exponent Not Dimensionless')).toBeVisible();
});

test('Test function notation with integrals', async () => {
Expand Down

0 comments on commit 77dcdbc

Please sign in to comment.