Comments (13)
It looks like the I'm not sure what the round
built-in function uses the UnitScalar.__int__
method on Python 2round
built-in is using on Python 2 but on Python 3, it tries to use the u.__round__
method, which doesn't exist on an UnitScalar
object.
from scimath.
I'm not 100% convinced that rounding makes sense for values with units, or at least, not in a way that fits the round
function: it doesn't make sense to round a length to two decimal places, while it does make sense to round a length to the nearest cm / km / inch / 1/8 inch, etc.
How about a utility function round_to_multiple_of(value, target_unit)
(name to be bikeshedded later) that essentially does round(value / target_unit) * target_unit
?
from scimath.
@JCorson Question about your expectations. If you have:
>>> from scimath.units.api import UnitScalar
>>> u = UnitScalar(12625, units="m")
>>> v = UnitScalar(12.625, units="km")
then u
and v
are the same length, just with different representations:
>>> u == v
UnitScalar(True, units='None')
What would you want round(u, 2)
and round(v, 2)
to return in this case? (Or even just round(u)
and round(v)
?) I'd expect that since u
and v
are equivalent, the results of round(u, 2)
and round(v, 2)
should be equivalent, too.
from scimath.
Question about your expectations.
Understanding the usecase will definitely help us in deciding whether or not to support or how to support this functionality.
from scimath.
round
of course does make sense in the special case of unit-less quantities, and it wouldn't be unreasonable to support that case.
from scimath.
Sorry about that, I should have been clearer on the use case. We have been using UnitScalars in the project and many tests use assertAlmostEqual
. This worked in Python 2, but the tests are all failing in Python 3 with the aforementioned TypeError
. The example code was just to reproduce the exception.
from scimath.
I'd consider the fact that this works in Python 2.7 to be a bug in scimath
: before we even get to round
, this depends on the ability to compare a unitfull quantity with a unitless quantity, which seems to me as though it should be a TypeError. It leads to fun situations like this:
>>> x = UnitScalar(2.0, units="ft")
>>> y = UnitScalar(1.0, units="m")
>>> z = 1.5
>>> x < y
UnitScalar(True, units='None')
>>> y < z
UnitScalar(True, units='None')
>>> z < x
UnitScalar(True, units='None')
The other part of this is due to the existence of __float__
, which provides an implicit coercion path that Python 2's round
uses but Python 3's round
doesn't. (I'd argue that it's questionable to be able to convert a unitfull value to a float
, too, but removing that ability would probably break code.)
from scimath.
To be clear, I'm a strong -1 on the idea of supporting round
for non-unitless quantities.
from scimath.
Make sense, and either way works for me. We have already gone through and made all tests use assertEqual
so this isn't blocking anything. FTR our use case was not to compare UnitScalars to floats with assertAlmostEqual
, but UnitScalars to UnitScalars. So something like:
exp_value = UnitScalar(10.1234, units='gram')
self.assertAlmostEqual(value, exp_value, places=4) # where value = UnitScalar(10.12340000000001, unit='gram')
It is easy enough for us to just right a function to do the comparison that we want.
from scimath.
FTR our use case was not to compare UnitScalars to floats with
assertAlmostEqual
, but UnitScalars to UnitScalars.
Yep, that's what I assumed you meant. When you do that, the underlying code first subtracts those two UnitScalar values (getting another UnitScalar as the difference), and then compares that difference (or rather the absolute value of the difference) to a float, in this case 1e-4
(from the places = 4). That's the comparison I was talking about that I don't think makes sense.
I'd suggest a custom assertion self.assertEqualToWithin(value1, value2, diff)
, where all of value1
, value2
and diff
are comparable "unitful" quantities. E.g., I want to know that my calculated critical dimension is equal to the expected critical dimension to within 1nm, say.
from scimath.
I'd suggest a custom assertion
self.assertEqualToWithin(value1, value2, diff)
, where all ofvalue1
,value2
anddiff
are comparable "unitful" quantities. E.g., I want to know that my calculated critical dimension is equal to the expected critical dimension to within 1nm, say.
Yup. Sounds good to me.
from scimath.
I had some general assertion functions to do what you want I believe so I pushed it as a PR: #85 .
from scimath.
I also opened an issue #86 for the comparison of incomparable quantities.
from scimath.
Related Issues (20)
- Remove "scimath.units.unit_converter.convert_quantity_old" HOT 1
- Improve module docstrings in "scimath.units" HOT 1
- Address deprecation warnings raised in the testsuite HOT 1
- Warnings when compiling "scimath.interpolate._interpolate" HOT 2
- Have unit names with `ยต` for micro- prefix and allow other symbolic unit names
- Can "scimath.units.unit_array.__newobj__" be removed? HOT 2
- Remove `*`-imports or use `__all__`
- Use "observe" instead of "on_trait_change" HOT 1
- [5.0.0] Release Checklist
- Update "setup.py" to ship release candidates
- Test fail with Python 3.8
- Update copyright header dates
- Investigate compatibility with Python 3.11 HOT 13
- Update documentation of scimath to include test environment setup HOT 4
- Add new SI prefixes HOT 1
- When we test pypi with python 3.6 and ubuntu 22.04, a architecture not found error will happen HOT 2
- Tracebacks in test output HOT 5
- Github actions: add doc generation workflow HOT 1
- Error during installation HOT 2
- Bleeding edge workflow reinstalls packages into the bootstrap edm environment, not the dev environment
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scimath.