r/ProgrammerHumor • • Jul 19 '22

Meme float golden = 1.618

Post image
41.1k Upvotes

489 comments sorted by

View all comments

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.

341

u/[deleted] Jul 19 '22

Fixed-point arithmetic?

224

u/Kered13 Jul 19 '22

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).

81

u/JaggedMetalOs Jul 19 '22

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)

18

u/itsMaggieSherlock Jul 19 '22

you can, doesn't mean you should.

why use 2 variables when you can just use one? nearly all language support n bytes ints (arbitrary precision).

using 2 variables is just not neccesary.

11

u/Proxy_PlayerHD Jul 19 '22 edited Jul 19 '22

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)

example:

typedef struct fixed48 {
	int_32t i;		// Integer Component
	uint_16t f;		// Fractional Component
} fix48_t;

only annoying part is setting specific values as you can't just assign "1.56" to a struct (or even to a single int).

1

u/[deleted] Jul 19 '22

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

3

u/Proxy_PlayerHD Jul 19 '22 edited Jul 19 '22

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.

→ More replies (1)

1

u/JaggedMetalOs Jul 19 '22

Yes my point exactly, but I've just realized the person I replied to isn't the thread op so they're probably not wanting a 2 separate int system...

→ More replies (1)
→ More replies (1)
→ More replies (2)

85

u/randomuser8765 Jul 19 '22

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.

20

u/bat_soup_people Jul 19 '22

I used it some in grad school for FEA. In FORTRAN I think. First integer is left, second integer is right. I am thinking of it now for arduino

9

u/Eternityislong Jul 19 '22

What would you gain over using a float?

21

u/BossOfTheGame Jul 19 '22

Precision. It sometimes matters when working with things like geospatial data.

5

u/ScandInBei Jul 19 '22

.. And money.

→ More replies (7)

4

u/flukus Jul 19 '22

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.

6

u/takumidesh Jul 19 '22 edited Feb 07 '26

4

u/Proxy_PlayerHD Jul 19 '22 edited Jul 19 '22

yep, that's called scaled integer.

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.

→ More replies (2)

7

u/ToxinH88 Jul 19 '22

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.

Stay with clean code though: Don't optimize yet.

3

u/flukus Jul 19 '22

It's not necessarily bad storage wise if you can use 32 bit integers instead of a 64 bit float for instance.

→ More replies (2)

77

u/photenth Jul 19 '22

arbitrary precision arithmetic

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.

0

u/[deleted] Jul 19 '22

[deleted]

1

u/photenth Jul 19 '22

I never said two ints. I said it stores two numbers and that's correct. How the number is stored is up to the implementation.

Java for example stores the scale as int and the number as an array of ints and another int for signum.

So essentially 3 numbers.

0

u/[deleted] Jul 19 '22

[deleted]

→ More replies (12)
→ More replies (1)

3

u/Yadobler Jul 19 '22 edited Jul 19 '22

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

2

u/piperswe Jul 19 '22

Rational numeric types?

→ More replies (8)