zen-lang / zen

Library for model driven systems
115 stars 12 forks source link

Add fixed-point precision (or format) validation to zen/number #22

Closed qdzo closed 2 years ago

qdzo commented 2 years ago

It is suitable to have constraints on decimal numbers like:

Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Or maybe add format like in "##.#"

https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

carbon-hvze commented 2 years ago

https://github.com/zen-lang/zen/blob/master/pkg/zen/tests/types-test.edn#L441

carbon-hvze commented 2 years ago

@qdzo when you have a chance to test the implementation please get back to me so I can close the issue

qdzo commented 2 years ago

Hi, @carbon-hvze . Thanks for fast feedback.

Checked implementation and found interesting issue with leading zeroes and trealing zeroes after dot.

Clojure's reader drop trailing zeroes, and leave only one before dot

10.000 -> 10.0

and also drops leading zeros.

0001 -> 1

and if we set :scale 2 or :precision 4 and pass value 10.00 validation will fail in both scenarios. and with :precision 2 - if we pass 01 we also will get validation fail.

Maybe we need edge case here.

Everything else works great

carbon-hvze commented 2 years ago

after the discussion we agreed upon the following:

  1. for trailing zeroes use case - implement a rule that allows arbitrary number of zeroes after the dot (e.g. 10.0 will pass for scales 1,2,3, etc)
  2. for opening zeroes use case do not do anything as of right now
carbon-hvze commented 2 years ago

@qdzo hey please take a look at the trailing zeroes logic, if it suits your needs I will go ahead and close the issue

qdzo commented 2 years ago

Tested. Everything as expected. Thanks.

carbon-hvze commented 2 years ago

nice! closing the issue then

qdzo commented 1 year ago

We had to allow missing trailing zeroes after decimal point - because JSON remove all trailing zeroes and convert float to integer.

Example: 1.0 -> 1

And we don't have any way to prevent that behaviour.

qdzo commented 1 year ago

1ee4d37afdb625db4d28202043f2d608631b91ec also related to this problem.

Fixed validation of integer value when have scale = 1