Why do programming languages usually not implement number types with units?

  • Thread starter Thread starter elcaro
  • Start date Start date
  • Tags Tags
    Programming Units
AI Thread Summary
Programming languages typically do not implement number types with units due to the complexity and variability of unit systems across different applications. While Frink is an exception, most languages leave unit management to the programmer, often relying on naming conventions and class implementations for conversions. The discussion highlights the importance of consistent units in physical modeling, suggesting that unit conversions should occur in the presentation layer rather than being embedded in the language itself. Libraries are favored for handling unit conversions, as they can be tailored to specific needs without complicating the core language. Overall, the consensus is that intrinsic unit types could introduce more issues than they resolve, advocating for a library-based approach instead.
  • #51
Vanadium 50 said:
My understanding from reading the description is that this would be perfectly happy adding an energy to a torque.
In F# units of measure you get an error if you add types which are not of the same measure of convertible to the same measure.

https://fsharpforfunandprofit.com/posts/units-of-measure/
 
Technology news on Phys.org
  • #52
Rive said:
Maybe, but not necessarily. Somewhere above (life) critical systems were mentioned.
Those usually does not have the newest CPU and top notch hardware. Just something reliable.Yeah. Less code, more documentation and engineering :doh:
Especially in case of 'critical' systems.
But if you have that, then why is this whole thing needed?

The only place I can think it would be slightly useful is for some specialized physics- or engineering oriented language (for beginners/ non-programmers).
This comment is ludicrous.
 
  • #53
jbergman said:
This is completely wrong.
Which part?

Is it that things are completely clear? Not to me!

Is it that checking dimensions is not the same as checking units? I think that's self-evident, but in any event examples have been provided where this does not work.

Is it that we can achieve this today without changing intrinsic variables? Again the C++ examples have been discussed.

"You're just wrong" is not so helpful.
 
  • #54
jbergman said:
This comment is ludicrous.
I can imagine that some would see so.
Care to elaborate?
 
  • #55
I don't know if I'm hi-jacking the thread, but I would prefer to have a language that can carry the error throughout the calculations before one taking care of units. Something like the inputs are 12.34 (±0.01) and 1.234 X 106 (±1000), then [calculations, calculations, calculations], and the output turns out to be 5.32846895031784 (±0.1). This way, I know that the final answer is 5.3 and all other decimals are superfluous, except if used in other calculations. You change the accuracy of the inputs and your answer might become 5 or 5.3285.
 
  • Like
Likes Jarvis323
  • #56
jack action said:
I don't know if I'm hi-jacking the thread, but I would prefer to have a language that can carry the error throughout the calculations before one taking care of units. Something like the inputs are 12.34 (±0.01) and 1.234 X 106 (±1000), then [calculations, calculations, calculations], and the output turns out to be 5.32846895031784 (±0.1). This way, I know that the final answer is 5.3 and all other decimals are superfluous, except if used in other calculations. You change the accuracy of the inputs and your answer might become 5 or 5.3285.
Seems a usefull addition. It is part of extended numeric types that carry information one needs when doing calculculations in for example physical models. Including the unit of measure and the error.
 
  • #57
jack action said:
I would prefer to have a language that can carry the error throughout the calculations before one taking care of units.
Do you need it to be a change in how intrinsics behave or can it be a class?

Given that error propagation can be non-trivial, it is a lot easier to do as a class.
 
  • #58
Vanadium 50 said:
Given that error propagation can be non-trivial, it is a lot easier to do as a class.
I agree it is not trivial. Usually it is the work of mathematicians to apply theory based on the numerical algorithms, to get error bounds on things like matrix operations.

But then you also have the issue that high level code may not compile to what you expect.

If you have error propegation in a class, then the class has to know the hardware it will run on, and how the code the compiler will generate, in addition to being able to apply complex global analysis of the dataflow and algorithms.
 
  • #59
Jarvis323 said:
the class has to know the hardware it will run on
Why?

Why is arithmetic used in error propagation different from any other use of arithmetic?
 
  • #60
Vanadium 50 said:
Do you need it to be a change in how intrinsics behave
I don't know if I need it, but it would be nice if, when I write an equation with basic expressions, the program identifies the error based on how the number is written (say ±1 on the last significant digit) and gives the final answer rounded up. How idiotic is it when you get a float set to 3.00000000000000008 as an answer? It is literally a wrong answer. I think basic computing could correct that very easily. And getting 3.0 or 3.0000, instead of 3, would add meaning to the number.

I've done the unit conversion thing because it is something that bothers me as well. I've tried to do the error propagation, but it is much more complex to do (in a general way) without replacing all expressions (say, 'a + b' becomes 'add(a, b)') and all your programs become much harder to read (and write).
 
  • #61
Vanadium 50 said:
Why?

Why is arithmetic used in error propagation different from any other use of arithmetic?
As an example, some compilers/platforms have compiler flags to do reduced precision floating point operations or approximations.
 
  • #62
You're talking about significant figures, which is a quick-and-dirty reflection of real error propagation. If you build that into your intrinsics, won't it make it that much harder to program in the more correct procedure?

I confess that I find the logic of this thread puzzling. What is proposed can be done in various flexible languages. But no, that's not good enough. It has to be by changing how the language deals with intrinsic types, often in incompatible ways.
 
Last edited:
  • Like
Likes pbuk
  • #63
Jarvis323 said:
I agree it is not trivial. Usually it is the work of mathematicians to apply theory based on the numerical algorithms, to get error bounds on things like matrix operations.
Yes. Things - again! - burns down to the 'engineering' part of the software development.

Jarvis323 said:
But then you also have the issue that high level code may not compile to what you expect.
Quite a bullseye. All these fancy additions/modifications would be a nightmare to validate.
 
  • #64
Vanadium 50 said:
won't it make it that much harder to program in the more correct procedure?
I guess I'm talking more about what a floating-point represents versus an integer.

Math with integers is easy: 1 is exactly 1, 3 is exactly 3, thus ##\frac{1}{3} = 0.\bar{3}##. It works very well in theoretical work.

But with more practical problems, you rarely work with such certainty. You deal with some level of precision and you have 2 limits: the significand length that the program can handle and the precision of the inputs. You cannot escape the former but, somehow, nobody cares about the latter. To me, ##\frac{1.0}{3.0} \neq \frac{1.00}{3.00}## and neither of them is equal to ##0.\bar{3}##. I would at least expect an answer that doesn't have more than 2 or 3 significant figures. But to be able to perform other calculations, having the error following the number is crucial.

It would be nice if a computer program could keep track of this throughout its calculations. So a floating-point would store a significand, an exponent, and an error.
 
  • #65
So in summary, we have the following requirements for floating point numerics:
  1. they should have unit information attached so we can't add feet to metres
  2. they should have dimensional information attached so we know that a velocity times a time is a distance
  3. they should have physical information attached so we can't add a torque to an energy
  4. they should have measurement error attached (according to what standard?) so we can present results appropriately
  5. they should have round-off error attached so we can track its propogation
  6. they should have truncation error attached so we can track this without having to understand the underlying algorithm (nobody mentioned this but I thought I'd add it for good measure)
I think it should now be obvious why these requirements are not implemented in general purpose languages, however subsets of these requirements are implemented in certain special purpose languages and also in modules for certain general purpose languages. Also, where a system is highly specialised and safety-critical, the general features of many special purpose languages can be used to implement numeric classes implementing whatever subset of these features is appropriate.

Mods, are we done?
 
  • Like
  • Sad
  • Skeptical
Likes Jarvis323, BvU, jack action and 1 other person
  • #66
pbuk said:
Mods, are we done?
Seems like it to me ...
Thread closed
 
  • Like
Likes BvU
Back
Top