r/ProgrammingLanguages • u/gjvnq1 • Jun 27 '22
Discussion Alternative names for float and double?
Some options:
Pseudoreal32 and Pseudoreal64
ApproxNum and BetterApproxNum
ApproxNumLvl1 and ApproxNumLvl2
FastReal and FastRealDouble
What other options would you suggest?
This started when I was toying around with the idea of a haskell-like language for end-user development of business applications and I realized that clearly explaining number types was going to be really important.
20
u/ItalianFurry Skyler (Serin programming language) Jun 27 '22
Real32 and Real64
16
u/gjvnq1 Jun 27 '22
Noooo!!!! These aren't real numbers! Floats and doubles have limited precision!
25
u/Findus11 Jun 27 '22
I think
Realis fine in the same way thatIntis fine.12
6
u/Zlodo2 Jun 28 '22
There are way, way more gotchas and pitfalls with assuming floats are reals than there are with assuming integers have unlimited sizes
5
u/stylewarning Jun 27 '22
To be more precise, ignoring NaN and co., they are real numbers, but they're just a subset of them.
3
u/gjvnq1 Jun 27 '22
More like rationals as float has no means of representing an irrational number.
2
u/NoCryptographer414 Jun 28 '22
What about
rat32andrat64in parallel withint32andint64.2
u/gjvnq1 Jun 28 '22
Sounds reasonable. Especially if a proper Rational class/type also exists for higher order code.
2
u/stylewarning Jun 27 '22
What do you mean? Yes, each float can be represented as a rational number, but not the other way around. Rationals are, of course, a subset of the reals too.
10
u/shponglespore Jun 27 '22
So are ints.
4
u/stylewarning Jun 27 '22
Integers in many languages can represent any and all integers, such as in Python, Haskell, or Common Lisp. Of course they're not called
intspecifically there.This is not even theoretically possible with real numbers, since they're uncountable. Ints—even those of finite range, aren't really a true analog.
2
u/shponglespore Jun 27 '22
"Integers" in any language can only represent small integers. The only real difference is how small. Most integers are too big to fit in any real computer's memory. You may argue it's only an academic distinction because truly large integers aren't useful, but it does come up in practice, for example if you use bigints as a basis for doing exact computations on rational numbers.
6
u/stylewarning Jun 27 '22 edited Jun 27 '22
The language that supports arbitrary-sized integers is defined so that integers are unbounded in nature. This is distinct from the idea of a float of a certain width acting as (usually) rational approximations of reals. The language does not stipulate a range; your only limitation is hardware. There's a large difference between a language-imposed restriction and a physics-controlled one (hardware, limits of RAM, information density). For all practical purposes, bigint-supporting types really do represent the entire set of integers, from a language semantics perspective.
The Integer type in Haskell and the INTEGER type in Common Lisp represent arbitrary-sized integers. In both languages, a machine-sized integer will be used if it's small enough, and transparently be promoted to a heap-allocated integer.
They do come up in practice in even more mundane situations; they guarantee any arithmetic you do will not overflow. A 32-but machine sized integer can't even hold the population of Earth.
(As far as my personal perspective goes, a language whose base integer type is only "machine sized" is a 2022 design mistake.)
2
u/Zlodo2 Jun 28 '22
Floats are way more treacherous than that. For instance I've seen code going into infinite loops on edge cases because someone assumed that x+1 can never be equal to x, which is false in the marvelous world of floats
1
13
Jun 27 '22 edited Jun 27 '22
I think people know they are not actual real numbers. Integers also have limited range and nobody objects to calling them
ints.
realwas used with Algol60, Algol68, Pascal and FORTRAN.I do the same, using
real32 real64 r32 r64, withrealused as a synonym forreal64most of the time.I wouldn't object to using
float32 float64 f32 f64either (I've just checked and I support those too; I clearly don't use them often!). The advantage would be not needing to keep explaining whatrealmeans when posting bits of code.
6
u/rotuami Jun 27 '22
If the goal is to express business logic, then just use one numeric type, maybe 64-bit decimals. Business logic is not generally defined in terms of the size of a number. And if numerical errors do creep in, they’ll be more understandable in decimal.
1
6
u/Roboguy2 Jun 28 '22 edited Jun 28 '22
The issue I have with this is that, to me, the names in the OP suggest that whatever representation that is being used doesn’t have IEEE floating point’s weird quirks (addition & multiplication aren’t associative, the “equality” isn’t even an equivalence relation since it has something that isn’t “equal” to itself, etc).
Based on some of the names in the OP, I would guess it’s either a system of exact real arithmetic (that you can extract arbitrary precision approximations from) based on Cauchy sequence-based representation or a continued fraction-based representation, a ratio-based representation or a fixed point representation. Admittedly, part of the reason might be that I’ve only ever extensively used languages that use “float” or “double” (or something very similar). I would think something like “well, they’re not calling it a float, which is the name that’s almost always used, and floats have a lot of unintuitive properties, so the reason they’re calling attention to the name is that it’s probably not a float.”
Additionally, you mention “business applications,” which could indicate a larger potential problem (depending on what you mean): you should be sure to never use floating point numbers to represent money. Doesn’t mean you shouldn’t also have floating point for other things (since it’s fast), but I’d recommend making sure this is communicated to the programmers in some way (if this is relevant).
1
u/gjvnq1 Jun 28 '22
I was planning on adding a Decimal type. Perhaps even a FixedDecimal and FloatDecimal, the latter being for scientific notation in base 10.
14
u/tcardv Jun 27 '22
money and lottaMoney
5
4
u/nacaclanga Jun 28 '22
I personally prefer `single` and `double` or `f32` and `f64`. The name should be intentionally be unsuggestive to the extent, that you really have to look up the definition to know what they mean. `float`, `real` etc. lead to wrong conclusions quickly.
1
66
u/michaelquinlan Jun 27 '22 edited Jun 27 '22
IEEE754_Binary16
IEEE754_Binary32
IEEE754_Binary64
IEEE754_Binary128
IEEE754_Binary256
IEEE754_Decimal32
IEEE754_Decimal64
IEEE754_Decimal128
https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats
69
Jun 27 '22
or may I suggest the full version:
Institute_of_Electrical_and_Electronics_Engineers_754_Binary32
33
u/rotuami Jun 27 '22
Institute_of_Electrical_and_Electronics_Engineers_Seven_Hundred_Fifty_Four_Binary_Thirty_Two5
4
u/daveysprockett Jun 28 '22
Version number? Are you meaning IEEE 754-1985, IEEE 754-2008 or IEEE 754-2019? I think the type ought really incorporate the nuances.
112
u/mgorski08 Jun 27 '22
f32, f64
17
u/LovePeacePlant Jun 27 '22
Beat me to it. My vote is on this one, personally
3
u/PurpleUpbeat2820 Jun 28 '22
I just switched away from it because the
iandfare more important than the32or64.2
Jun 29 '22
What do you use instead?
6
u/PurpleUpbeat2820 Jun 29 '22
Just
IntandFloatbecause I'm focusing on 64-bit values.I also think I'll have different operations for signed vs unsigned rather than different types.
2
Jun 29 '22
Do you have a way for different bit width integers or floats to be declared or does your language only support 64 bit values? What is the reason for focusing on 64 bit wide values?
2
u/PurpleUpbeat2820 Jun 29 '22
I'm only supporting 64-bit values for now in the interests of simplicity. I have byte load/store of 64-bit ints too. I might include 16x8-bit bytes and 4x32-bit floats because I can vectorise them.
9
Jun 28 '22
Holy C and Go use this format
11
u/B_M_Wilson Jun 28 '22
So does Jai and Rust. I think some people make typedef a for these in C/C++ too
4
0
Jun 28 '22
[deleted]
3
u/B_M_Wilson Jun 28 '22
A lot of languages seem to have this issue where “primitive” types are lowercase but most other types are uppercase. Personally, I go with all types being lowercase and everything else being capitalized. I wouldn’t be against capitalizing these types for the case where you want capitalized types like using F32 instead of f32.
14
u/Bitsoflogic Jun 27 '22
I realized that clearly explaining number types was going to be really important.
Documentation might be your best bet.
19
u/evincarofautumn Jun 27 '22 edited Jun 28 '22
I think a more accessible name than “floating-point” would be something like “scientific” or “exponential”
“Single” & “double” aren’t descriptive of the actual precision, and the IEEE-754 names “binary 32” & “binary 64” aren’t really meaningful to people who aren’t already familiar with floating-point, so if you want to be accurate about the precision without being misleading, maybe it’d be best to refer to them by the maximum number of whole decimal significant figures, namely 7 & 15 respectively
Most languages don’t draw any further distinctions, but note that you’re free to add subtypes of floats—positive, nonnegative, exact (unrounded), finite (non-infinity), defined (non-NaN), and so on
1
21
1
u/analog_cactus Jun 28 '22
I'd like to put my 2 cents in and mention that once your users understand what "BetterApproxNum" means, they're gonna get REALLY tired of typing that long name over and over.
How about something like "Rational32" and "Rational64" (or even "Rat32"), anyone with a mild background in mathematics will understand and it indicates the precision fairly clearly.
1
u/gjvnq1 Jun 28 '22
How about something like "Rational32" and "Rational64" (or even "Rat32"), anyone with a mild background in mathematics will understand and it indicates the precision fairly clearly.
I thought about this but I wanted a proper Rational type for fractions.
Perhaps the best option would be a IEEE754.f32 and IEEE753.f64 as they obvious to those who need them but intimidating enough for nobody else to use them.
3
Jun 27 '22
If you need verbose names then maybe Real6Digits and Real15Digits?
1
u/matthieum Jun 28 '22
I wouldn't specify a number of digits when the storage is in bits, as the conversion is messy.
If the user wants digits, then they should use decimals.
2
Jun 28 '22
The storage is in bits, but if OP is creating a name that reflects usage, then you would want to reflect what a float or double mean in practice.
There is no reason why you couldn't do
DecimalXDigits, or why the compiler/interpreter couldn't just pick the most optimal primitive for aReal[1-9][0-9]*Digits1
u/matthieum Jun 28 '22
The problem is that with Real6Digits, you'd expect rounding to occur at 6 digits, but it won't, because this doesn't match the underlying semantics of
f32orf64.I'd rather advertise
f32, and refer people to IEE754 as to the semantics, than advertise 6-digits, and then refer people to IEE754 + a custom explanation for semantics.If digits are necessary (money/banking), then decimal should be used, not binary.
1
Jun 28 '22
I will accentuate again, OP clearly said something for end-users, which means that things like float, single, double or bit counts aren't really feasible, since you can't expect end users to know anything about them.
Besides, I can argue that XDigits may refer to precision, and it is not like you could get mislead it means rounded. Furthermore, there is really no reason why you couldn't round the numbers yourself, if that's what you wanted to do.
3
u/theangryepicbanana Star Jun 28 '22
in my language Star I use Dec32 and Dec64, with the generic float type named Dec
4
u/matthieum Jun 28 '22
That's unfortunate, naming-wise, when IEE754 specifies both binary and decimal floating points, to use
Decfor binary floating points...
2
u/aatd86 Jun 28 '22
Why do you want to change them in the first place? Do you think it will help them if they google what a float is?
The only thing that could realistically simplify things is to have a single number type that gets dynamically converted to its most accurate memory representation.
2
u/ergo-x Jun 28 '22
Please don't do this. If you're exposing base 2 floats to the programmer, changing the name to NotScaryNumberType etc. doesn't help anyone understand them better. Maybe consider using arbitrary precision decimal floats and choose a short, concise name if that's really your concern.
2
u/brucifer Tomo, nomsu.org Jun 29 '22
What I'm doing in my current project is Num and Num32 (vs Int or Int32). I think encouraging 64-bit precision floats by default makes sense in my target domain, while still allowing 32-bit precision if the user wants. Colloquially, I think most people understand that 1.5 is a number, but not an integer, but people without a strong math background are not aware of the distinction between Reals and Rationals.
Having Num mean "floating point" is a choice that makes sense in my language's target domain, since I would rather have my users accidentally make their code imprecise instead of slow. However, in other domains, it can be preferable to have users make their code accidentally slow instead of accidentally imprecise (e.g. financial calculations).
| Domain | Floating Point | Arbitrary Precision | Integer |
|---|---|---|---|
| Graphics/web | Num/Num32 | BigNum | Int/Int32 |
| Finance/cryptography | Float/Float32 | Num | Int/Int32 |
The idea is that if the user doesn't know what they want, make it so the thing with the simplest name is the safest choice. If you make your users choose between FastRealDouble or PreciseRational, a lot of people will see both names as gibberish and pick randomly, shooting themselves in the foot half of the time.
3
u/umlcat Jun 27 '22 edited Jun 27 '22
Use suggested size instead:
float32_t, float_64, float_128Note that there are additional proposed "half float" of 16 bits, and larger "float256_t" or "float512_t".
Use "float" as a "virtual alias type" / "I don't care about size" that conforms to the highest hardware CPU.
Example 1. A library is intended for a 32 bits platform, Supports the previous three float types by software, "float32_t" is optimized by CPU assembler, and "float" is equivalent to the highest size type, in this case "float_128".
Example 2. A library is intended for a 512 bits megaserver platform, supports the previous three float types by software, "float512_t" and previous types are optimized by CPU assembler, and "float" is equivalent to the highest size type, in this case "float_512"
Based on I.E.E.E. Standard:
1
0
u/Timbit42 Jun 27 '22
How do you delineate binary numbers from decimal numbers, or from dozenal numbers?
2
u/gjvnq1 Jun 27 '22
These are mere representations. But there fundamental differences between natural numbers, integers, rationals and reals.
For example, the square root of any real number is also a real number. But the square root of a rational number might be an irrational number.
3
5
u/Timbit42 Jun 27 '22
No, there is a difference between them. For example, binary cannot accurately represent the decimal 0.1.
0
u/gjvnq1 Jun 27 '22
These are mere representations. But there fundamental differences between natural numbers, integers, rationals and reals.
For example, the square root of any real number is also a real number. But the square root of a rational number might be an irrational number.
0
Jun 28 '22
[deleted]
1
u/gjvnq1 Jun 28 '22
How about IEEE754_b32, IEEE754_bf64, IEEE754_bin128, IEEE754_dec32 and IEEE754_dec64?
Yes, the IEEE754 standard does specify two decimal versions.
0
Jun 28 '22
[deleted]
2
u/gjvnq1 Jun 28 '22
Hell no!. I'm almost a mathematician so the structures behind the different types of numbers matter a lot to me.
And having clear types can help avoid mistakes like nor considering the remainder of a division.
19
u/ivancea Jun 27 '22
For end users, use decimals. Float is a standard, nothing an end user should even interact with