News for hackers. Stuff that matters.
Re:A word for a value between 0 and 1 (inclusive) by codesections on Friday September 04, 2020 @06:31AM (#24373660)
Unfortunately, creating a constrained type like this isn't easy (or even doable) in all programming languages.

Fortunately, my preferred programming language, Raku, makes creating this sort of subset trivially easy[0]:

  subset UnitInterval of Real where 0 ≤ * ≤ 1
[0]: https://docs.raku.org/language/typesystem#index-entry-subset...
Re:A word for a value between 0 and 1 (inclusive) by tzs on Friday September 04, 2020 @11:25AM (#24375753)
In C, I wonder if you could do something with functions and macros?

Say you need to represent velocity in a transportation simulation. You could have a function, velocity, that looks like this:

  double velocity(double v, char * of_what)
You use it to wrap constrained values. E.g.,

  double v_jogger = velocity(8.0, "human");
  double v_car = velocity(65.0, "city car");
velocity() simply returns the first argument, after doing validity checking based on the second argument.

You probably couldn't reasonably use this everywhere that you would use actual constrained types in a language that has them, but you could probably catch a lot of errors just using them in initializers.

Re:A word for a value between 0 and 1 (inclusive) by saagarjha on Friday September 04, 2020 @01:39PM (#24377117)
The problem you'd have is that doing any operations on such a value could take it outside the bounds of the "type".
Re:A word for a value between 0 and 1 (inclusive) by Razengan on Friday September 04, 2020 @03:32PM (#24378159)
In Swift you can achieve this with Property Wrappers:

  @Clamping(0...14) var pH: Double = 7.0
https://nshipster.com/propertywrapper/#implementing-a-value-...
Re:A word for a value between 0 and 1 (inclusive) by johnsolo1701 on Friday September 04, 2020 @08:52AM (#24374442)
Is there a difference between ≤ and <= in Raku? Curious why you used ≤ in your example.
Re:A word for a value between 0 and 1 (inclusive) by winthrowe on Friday September 04, 2020 @09:50AM (#24374888)
They're equivalent, Raku defines both Ascii compatible and Unicode names for typical operators. I expect they used ≤ because it looks nicer.
Re:A word for a value between 0 and 1 (inclusive) by codesections on Saturday September 05, 2020 @12:53AM (#24381492)
Yep, correct on both counts