Replies: 10 comments 12 replies
-
Units do work with a lot of functions, but is not supported with We can consider adding support for units in It could work if we pass an additional argument to round to specify in which unit we would like to round, like |
Beta Was this translation helpful? Give feedback.
-
Would it be feasible to support rounding of |
Beta Was this translation helpful? Give feedback.
-
@joshhansen I'm not sure whether I understand you, so how would the caller resolve ambiguity? |
Beta Was this translation helpful? Give feedback.
-
I think what he is saying is that round() would accept only a single number and unit and not a complex expression So you would need to write like?
|
Beta Was this translation helpful? Give feedback.
-
Okay learned a bit more about how fixPrefix behaves... I was thinking maybe to have fixPrefix not be resetted as long as there is no operators that changed unit prefix. But I see now that if this flag is true then function format will not search for the best prefix but leave it as initially provided. So we cannot use that. I think we may need a separate but similar flag to track if the original prefix is preserved so far. Quick look at current behaviourHad a quick look at current behavior
Regarding when fixPrefix changes, it surprisingly change to false even if both side of an add function is the same units. This is as per
unit.fixPrefix is set to true unit.fixPrefix is passed along |
Beta Was this translation helpful? Give feedback.
-
Investigated what we can change to add ambigiousPrefix flag. I'm not familiar enough to change mathjs directly, but here is my observations. Files of interest for people to modify
Hypothetical usage example of a unit round functionThis example using my scripting calculator https://github.com/mofosyne/QuickMathJS that uses math.js as it's backend. Note this line # Range Quote Input
upper_bound_price = 50.33 AUD exc gst / pax
upper_pax_count = 30 pax
lower_bound_price = 55.55 AUD exc gst / pax
lower_pax_count = 10 pax
# Slope Intercept Formula (y = mx + c)
m(y1, y2, x1, x2) = ((y1 - y2) / (x1 - x2))
c(y1, y2, x1, x2) = y1 - m(y1, y2, x1, x2)*x1
y_linear(x, y1, y2, x1, x2) = m(y1, y2, x1, x2)*x + c(y1, y2, x1, x2)
# Food Price Per Persion Function
foodPricePP(paxCount) = y_linear(paxCount, upper_bound_price, lower_bound_price, upper_pax_count, lower_pax_count)
# Note that unit price is $ per person
foodPricePP(10 pax): 55.55 AUD exc gst / pax
# Ah here, this fails because round() doesn't support units... solved by using .toNumber() to remove units for now
foodPricePP_rounded(paxCount) = round(foodPricePP(paxCount).toNumber(), 2) * AUD exc gst / pax
foodPricePP_rounded(3 pax): 57.38 AUD exc gst / pax
# Formatted Output Functions
style = {notation: 'fixed', precision: 2}
foodPricePPout(paxCount) = format(foodPricePP_rounded(paxCount) , style)
foodTotalPrice(paxCount) = format(foodPricePP_rounded(paxCount) * paxCount , style)
# People Count To Per Person Price
foodPricePPout(10 pax): 55.55 AUD exc gst / pax
foodPricePPout(11 pax): 55.29 AUD exc gst / pax
foodPricePPout(12 pax): 55.03 AUD exc gst / pax
#... etc...
foodPricePPout(28 pax): 50.85 AUD exc gst / pax
foodPricePPout(29 pax): 50.59 AUD exc gst / pax
foodPricePPout(30 pax): 50.33 AUD exc gst / pax
# People Count To Total Food Cost
foodTotalPrice(10 pax): 555.50 AUD exc gst
foodTotalPrice(11 pax): 608.19 AUD exc gst
foodTotalPrice(12 pax): 660.36 AUD exc gst
#... etc...
foodTotalPrice(28 pax): 1423.80 AUD exc gst
foodTotalPrice(29 pax): 1467.11 AUD exc gst
foodTotalPrice(30 pax): 1509.90 AUD exc gst |
Beta Was this translation helpful? Give feedback.
-
Really interesting discussion! In UnitMath I've actually tried to remove "hidden" flags like So, unfortunately I don't have a good suggestion on how to round units automatically without providing the unit you want to round by. But, if you do know what unit you want to round by, you can do it pretty easily: x = 10.5 cm
round(x / cm) * cm // 11 cm |
Beta Was this translation helpful? Give feedback.
-
Thanks, made the change to #3091. Surprisingly passes even thought its says "deep Strict" rather than just "strict"? It's a little unintuative. |
Beta Was this translation helpful? Give feedback.
-
Support for units in function |
Beta Was this translation helpful? Give feedback.
-
Okay just double checked how it looks now, when integrated with quickmathjs... But this shall do as it's still better than having to use a hack/workaround! Thanks for getting this in! # Range Quote Input
upper_bound_price = 50.33 AUD exc gst / pax
upper_pax_count = 30 pax
lower_bound_price = 55.55 AUD exc gst / pax
lower_pax_count = 10 pax
# Slope Intercept Formula (y = mx + c)
m(y1, y2, x1, x2) = ((y1 - y2) / (x1 - x2))
c(y1, y2, x1, x2) = y1 - m(y1, y2, x1, x2)*x1
y_linear(x, y1, y2, x1, x2) = m(y1, y2, x1, x2)*x + c(y1, y2, x1, x2)
# Food Price Per Person Function
foodPricePP(paxCount) = y_linear(paxCount, upper_bound_price, lower_bound_price, upper_pax_count, lower_pax_count)
foodPricePP_rounded(paxCount) = round(foodPricePP(paxCount), 2, AUD exc gst / pax )
# Formatted Output Functions
style = {notation: 'fixed', precision: 2}
foodPricePPout(paxCount) = format(foodPricePP_rounded(paxCount) , style)
foodTotalPrice(paxCount) = format(foodPricePP_rounded(paxCount) * paxCount , style)
# People Count To Per Person Price
foodPricePPout(10 pax): 55.55 AUD exc gst / pax
foodPricePPout(11 pax): 55.29 AUD exc gst / pax
foodPricePPout(12 pax): 55.03 AUD exc gst / pax
#... etc...
foodPricePPout(28 pax): 50.85 AUD exc gst / pax
foodPricePPout(29 pax): 50.59 AUD exc gst / pax
foodPricePPout(30 pax): 50.33 AUD exc gst / pax
# People Count To Total Food Cost
foodTotalPrice(10 pax): 555.50 AUD exc gst
foodTotalPrice(11 pax): 608.19 AUD exc gst
foodTotalPrice(12 pax): 660.36 AUD exc gst
#... etc...
foodTotalPrice(28 pax): 1423.80 AUD exc gst
foodTotalPrice(29 pax): 1467.11 AUD exc gst
foodTotalPrice(30 pax): 1509.90 AUD exc gst |
Beta Was this translation helpful? Give feedback.
-
Is there a way to make units work with functions?
round(2.70 inches)
errors out.
Beta Was this translation helpful? Give feedback.
All reactions