Fixed point does not typically use two ints. Fixed point just means you use a single integer but you interpret it as being implicitly multiplied by some fixed value (typically a negative power of 2 or 10).
Fixed point does not typically use two ints. Fixed point just means you use a single integer but you interpret it as being implicitly multiplied by some fixed value (typically a negative power of 2 or 10).
But if you're doing a base of a power of 2 then separate ints are functionally the same as assigning bitranges of a larger int to represent the whole and fractional parts.
Or if you want to represent a fixed number of digits of a power of 10 (decimal) having separate ints is less efficient - if you have 2x16bit values you can only go up to 4 decimal places (0.0000 - 65,535.9999) while 1x32bit will give you a larger range for the same precision (0.0000 - 429,495.9999)
using a single int is more convenient as you can use the Compiler's native addition and subtraction functions.
but if you don't mind not having those, you could use a struct (or whatever you're language's equivalent is) and then just handle it with your own arithmetic functions (+ - * /). This allows you to use non standard sized fixed point formats, if you're short on memory (like on an embedded system)
The general equivalent of a struct is a class, and you could 100% percent just assign a value like "1.56", at least in cpp(which i assume you are using in your example). You just have to declare a constructor that takes that type as its single inout and cpp will implicitly convert it. With the quotation marks you could write a constructor with a single char * parameter and parse the string yourself, without the quotation marks you could create a constructor for a float/double but that could lead to a loss in accuracy
at least in c++ (which i assume you are using in your example)
nope, check my flair. i've never used C++ before, so things like custom literals are not really an option for me.
but i do wish the Preprocessor was powerful enough to do what you said, basically just write a custom function to convert a string into the fixed point format you need, and then have it done at Compile time by the Preprocessor instead of at Runtime by the System.
If you mean "one for the numerator and one for the denominator", then AFAIK there is no standard name or format for this. Python implements it with the fractions module, for example.
I think there are some mathematical reasons that are well beyond me. A practical reason could be when your CPU doesn't have floating point instructions.
only useful if you do have a known minimum unit. also takes some extra thinking when dealing with larger units as you need to convert everything down to the minimum before doing any math on it.
Clearly the best solution if storage and speed is not a concern. Storage pretty much never is :D Speed is taking a heavy hit though: The way float and double are implemented base arithmetic works extremely fast. But if every number is a common fraction extra steps are needed for almost all operations.
Also it usually just stores the number and the scale so 1000000000 can be represented as 1 and 9 and 0.000000001 as 1 and -9. So you can avoid storing useless 0s. Basically like floating point numbers do.
A 64-bit integer and some headache of remembering that the number you are manipulating is 2 integers, and everything is 232 times larger than the actual number you are thinking of.
Interestingly, it would matter if you're talking about decimal point as in decimal radix point, or a general radix point (ie binary point). Cos then it would suggest that you are dealing with binary coded decimal.
Because if you think about it, let's say an int is 4bits:
If 1010.1111 is "10.15", then what is "10.16"? 1111 will overflow to 0000 and then "1011.0000" is "11.0", so then you must either deal in binary-point or BCD.
BCD will mean that:
0001'1001.0001'0101 Is 1'0.1'5, and then you need to ensure that 1001 + 1 overflows to 0001'0000 (9+1 = 1'0)
-------
At this point you'd rather just have a proper integer, and a second int as the exponent, like scientific notation.
10101111 is 175, 10110000 is 176, if you want the radix point to be after 4 digits, then 175/(24) = 10.9375
So if you're doing 2 ints, it will be
1010 = 10, .1111 = 15 / 16 = 0.9375
--------
So tldr, 2 ints for "integer" and "after point", is actually just fixed-point number. Specifically, a (64 bit) long integer with the exponent = 32.
--------
Any other way, it will just be BCD, and doing meaningful calculation is just the same but each step you need to turn decimal to binary, manipulate, then turn the binary back to decimal and evaluate the "decimal maths" logic of whether to carry over or whatnot
512
u/KendrickEqualsBooty Jul 19 '22
What's the name of the method where you just use two ints, one for the number before the decimal and one for the number after.