Half a croissant, on a plate, with a sign in front of it saying '50c'
h a l f b a k e r y
Caution!
Contents may be not!

idea: add, search, annotate, link, view, overview, recent, by name, random

meta: news, help, about, links, report a problem

account: browse anonymously, or get an account and write.

user:
pass:
register,


                                                                                   

Please log in.
Before you can vote, you need to register. Please log in or create an account.

Dimensioned Variables

Numbers with units.
  (+8, -5)
(+8, -5)
  [vote for,
against]

Imagine you're writing a bit of computer code and you have tell pause for a second. Now, is it pause(1) or pause(1000) -- should the the parameter be in seconds or milliseconds?

What if you could put a unit in there so pause(1s) and pause(1000ms) are equivalent. Not a big idea, and not that exciting, but maybe something that would stop the odd bug from creeping into your code.

st3f, Apr 25 2008

Scientific Symbols Scientific_20Symbols
[afinehowdoyoudo, Apr 26 2008]

date_modify() http://www.php.net/...ion.date-modify.php
...works like that. [DrCurry, Apr 26 2008]

the Frink Programming Language http://futureboy.homeip.net/frinkdocs/
Has what you speak of. [Spacecoyote, Apr 26 2008]

My reinvention of this wheel https://docs.google...7q_39mgzw8rc4&hl=en
See gtoal's 2nd annotation below [gtoal, Apr 27 2009]

Hungarian notation https://en.wikipedi.../Hungarian_notation
Mentioned in my anno [notexactly, Apr 27 2019]

Wolfram Alpha: Trivial Arbitrary Unit Conversion https://www.wolfram...+per+furlong+in+mpg
A nice natural language capable general processing tool - here converting 300 litres per furlong into an equivalent Miles Per Gallon ratio. [zen_tom, May 01 2019]

[link]






       It seems to me the syntax should be something like:
'pause(1000,"S")'
or
'pause(1000,"MS")'
Where the unit of time is passed as a separate parameter.
  

       I think this would just lead to more coding errors than remembering the units are always in milliseconds, but...
phoenix, Apr 25 2008
  

       What [phoenix] said.   

       A better idea might be to just accept the fate of owning a poor attention span. I know I have... done something what?
daseva, Apr 25 2008
  

       I can see where this is useful for time, but what other variables would it apply to? Obviously, you may want to distinguish volts and millivolts, or atmospheres and pascals, but these aren't really the same in programming terms as milliseconds/seconds. However, [+] for the idea.
MaxwellBuchanan, Apr 25 2008
  

       I think this would lead to less errors as you would be forced to define your quantities. Even if there was only one unit (eg milliseconds) - being forced to suffix your time variable with ms would remind you what you were working with.
wagster, Apr 25 2008
  

       //you're writing a bit of computer code... or pause(1000)// sp. "pause(0x3E8)"
AbsintheWithoutLeave, Apr 25 2008
  

       //pause(1000ms)//   

       So, what happens if the number is a variable, say "q", then you get "pause(qms). And what if qms is another variable?
MaxwellBuchanan, Apr 25 2008
  

       If you have a variable 'q' in a strongly typed language then 'q' already has a type - either seconds or milliseconds or something else, but its unit is not ambiguous. So I don't think pause(q) is a problem, and pause(qs) is nonsensical whether you interpret it as the nonexistent variable 'qs' or as q seconds.   

       A problem can potentially occur when you use a literal value directly without checking what the function requires, and when that value could be ambiguous as to the units it's in.   

       As such, appending a type specifier to a literal number would help avoid problems. You could consider pause(1000ms) shorthand for pause( (time_milliseconds) 1000).   

       So as long as it only applies to literal values, I think it's a good idea. [+].
Srimech, Apr 25 2008
  

       Google Calculator does this. By coincidence we were talking about the same thing just last week in the yahoo 'compilers101' group. Shouldn't be hard to do, probably more useful for a calculator than a compiler though.
gtoal, Apr 25 2008
  

       pause.ms(1000)
Ian Tindale, Apr 26 2008
  

       //So, what happens if the number is a variable, say "q", then you get "pause(qms). And what if qms is another variable?//   

       Realise you have to replace all instances of q. Give up, go make tea. That's what I'd do.
wagster, Apr 26 2008
  

       The DateDiff and DateAdd functions in VB do this. To add 3 days to the current day, you use DateAdd("d",3,today()) and to add 3 years you use DateAdd("yyyy",3,today())
angel, Apr 26 2008
  

       cf. PHP: sleep() in seconds vs. usleep() in microseconds. As per SI, the base unit of time is the second and so that is what should be assumed, unless otherwise indicated.
vincevincevince, Apr 26 2008
  

       Seems a bit pedantic: aren't your variables or library functions already going to be in the format most used for the program or module ?
FlyingToaster, Apr 26 2008
  

       You've basically invented the Frink programming language [link].
Spacecoyote, Apr 26 2008
  

       Dimensional analysis can catch a lot of calculation mistakes, but it's hardly perfect. Learning to rely upon it can be a recipe for disaster since many equations require dimensionless scaling factors (the most common being 0.5, as in s=0.5*g*t*t).
supercat, Apr 27 2008
  

       Some time back I sketched out some ideas in this vein in more detail - then I found someone had already implemented something almost identical to what I had designed, which I think means that rather than feeling bad that someone had done it before, I should feel good that someone reinforced my gut feeling that this is the obvious way to go :-) See my link above and please follow it through to Mark Austin's "Alladin" system at umd.edu
gtoal, Apr 27 2009
  

       MyTime = TimeSinceY2KInSeconds();
Do {} While TimeSinceY2KInSeconds() < MyTime + 1;
  

       Apart from that, it's quite a nice idea to have an interface to the computer that is based on real-world units. Maybe on startup, it could determine the size of your monitor and figure out exactly how long a centimetre is for you - this must be on the cards for future touch displays.   

       The same goes for seconds (assuming the user and the machine are occupying the same relativistic frame of reference of course)
zen_tom, Apr 27 2009
  

       Several years ago, I wanted to do some programming that dealt with real-world units of measurement, but I didn't want to learn and switch to Frink. I thought of doing something like Hungarian notation [link] where, instead of prefixing a variable's name with an abbreviation for its type or intended usage, you suffix it with the unit symbol. For example: time_s, length_m. Like Hungarian notation, it would be just a naming scheme and communicate nothing to the compiler/interpreter. But (I have just realized) you'd quickly run into an issue with compound units where you need a way to represent exponents and division within the variable name (e.g. acceleration_m/s^2), which would be difficult because most languages don't allow slashes or carets in identifiers.   

       Anyway, I never got around to doing said programming, so I still haven't tried this system out. But it wouldn't directly solve the problem of what unit to use when passing a numeric value to a function, beyond that the function's parameter could be named in this scheme, and your IDE could then give you a hint of the parameter name when you type the call.   

       You could implement this sort of thing by defining a type for each unit, and how to automatically cast from one unit to another where they're in the same dimension. I feel like that could be the best simple way to implement it in many popular strongly typed general-purpose languages. But then how do you do something like GramsPerMeterSquared arealDensity = mass / (length * width) where mass is of type Grams and length and width are of type Meters? You could overload the math operators (if the language allows you to), but that would be cumbersome to do for every combination of units you could do math with, not to mention you have to make a type for every possible resulting compound unit. So maybe that's worse than my first idea.   

       Maybe it would be best to just embed Frink in other languages…
notexactly, Apr 27 2019
  

       Meh. I program quite a lot (in a very enthusiastic but amateur way), and I'd sooner just have a clear understanding of the numbers, rather than assigning units.   

       What I _would_ like, though, is better handling of 1-starting and 0-starting numbering. I do a lot of DNA-related software, and DNA sequences conventionally start from 1. This means that, for instance, if I want to use a modulo function to split a sequence into 100-base lines, I have to go from 1-starting to 0-starting, do the moduloing, then convert back to 1-starting.
MaxwellBuchanan, Apr 27 2019
  

       I've never used it, but doesn't Matlab do this ? (units of measurement, not 0/1 self-flagellation)
FlyingToaster, Apr 27 2019
  

       //dimensionless scaling factors// Doesn't the factor have the lowest dimension of count of integer type? As in, I will only count half of this.   

       In my mind all numbers must have the unit tuple or they are meaningless. [Max] In my mind counting is different from serializing/ordering and for that matter grouping, which can start from anywhere.
wjt, Apr 28 2019
  

       Indexing is the word I was looking for. It's all numbering, just which dimension.
wjt, Apr 28 2019
  

       VBA, IIRC, uses 1-indexing, so you could do all your DNA programming in VBA.   

       // Doesn't the factor have the lowest dimension of count of integer type? As in, I will only count half of this. //   

       I didn't understand any of that. Could you paraphrase?
notexactly, Apr 28 2019
  

       A number in it's rawest form is of the single unit, 'count' in the dimension of counting. Just as a second count is the unit of the dimension counting time.   

       1,2,3 1 count unit , 2 count units, 3 count units. Along the dimension of counting.   

       A dimensionless number (referring to all those specifically described dimensions) still has the intrinsic unit of count in the dimension of counting.   

       Don't get me started on grouping count units.
wjt, Apr 28 2019
  

       What if it isn't an integer—is it still a "count" then?
notexactly, Apr 28 2019
  

       True, are irrational, transcendental or even imaginary numbers of unit count?   

       I suppose it comes down to what it fundamentally means to count and the dimension it is done on. The imaginary having a complex of counting dimensions and the irrational and transcendental having such blurry plots that they need indexing labels on the ordinary counting dimension.   

       All three are counted on and with so yes but with the dimension of counting either viewed with pin-pointer names or split into partial components.   

       Nearly all numbers are a count (some are just labels) so must rest on some sort of counting dimension line.
wjt, Apr 30 2019
  

       As the tagline says, I can read that, but I'm not following too closely.   

       "Count" does make sense to me, though, for things like frequency (where 1 hertz = 1 occurrence/second, but more commonly considered "1 /second") and wavenumber (in waves/millimeter, but again more commonly just "/millimeter").
notexactly, May 01 2019
  

       Yes, Halfbakery is for everyone. Read and even if you don't know it, you'll learn something.   

       And yes, the cleverness comes when you know what your counting and how mathematics can translate/manipulate to show how things relate.   

       There must be new forms of real number dimensional counts not discovered and incorporated yet into the human set of real numbers.
wjt, May 01 2019
  

       At some level, I think NOIR classification would be a good addition to programming language type definitions. We think Integers are for counting, and Floats are for continuous measures, but this isn't always good enough. NOIR stands for Nominal, Ordinal, Immnngng..somthething, and Ratio - and describes the different uses a number might have - Nominal, for group-naming, Ordinal for placing into some kind of rank/order, Interval(just remembered!) is usage for your traditional counting situation, and Ratio is for continuous ranges of data. You can do different kinds of stats on Ratios, Intervals and Ordinals depending, and normally you can only label things with Nominal data. So this is less a data-typing based on the internal digital representation (which is what float, integer, byte etc give you) and more a data-typing based on how you intend on using the information.   

       Once you've NOIRed your information, it's a trivial exercise* to assign/map appropriate units - or even to auto-translate across unit types - allowing someone to calculate fuel efficiency in litres per furlong, by supplying fuel consumed in metric ounces and distance in terms of light-picoseconds.   

       *by which I mean, bloody difficult in practice - but Wolfram Alpha/Mathematica does pretty good job - see link.
zen_tom, May 01 2019
  

       Isn't it also trivial to do that using the command line program 'units'?
notexactly, May 01 2019
  

       I mostly just name functions and variables to include critical units, but a third opportunity for world type safety comes to mind - reflection. Languages like C# have the ability to figure out what the ASCII name of variables and functions are, so when running in a debug mode, a program could kludge a safety check my making sure the function name and parameter variables match e.g. minutes = GetMilliseconds() is a runtime error under aggressive kludge debug.
bigsleep, May 02 2019
  

       You could write a library to do that. But would it know to not throw an error on, e.g., minutes = getMilliseconds() / 60000? My first thought was to go full dimensional analysis and somehow give the divisor a unit of milliseconds/minute, but how do you do that with a literal in your reflection-based scheme, because it doesn't have a name?
notexactly, May 02 2019
  

       //but how do you do that with a literal in your reflection-based scheme//   

       The code is available under reflection too.
bigsleep, May 02 2019
  

       Yes. How does that help know what units the literal has?
notexactly, May 02 2019
  

       Well I got your example, it was minutes is equivalent to 60000ms - so pass. But that's type safety for you, its just a guard and not a guarantee.
bigsleep, May 02 2019
  
      
[annotate]
  


 

back: main index

business  computer  culture  fashion  food  halfbakery  home  other  product  public  science  sport  vehicle