ProgrammerHumor

lemmeGoWithFixedPoint

lemmeGoWithFixedPoint
https://i.redd.it/os1p35n6s2df1.jpeg
Reddit

Discussion

calculus_is_fun
:js::p::bash:

0.2 -> 3,602,879,701,896,397 / 18,014,398,509,481,984 > 0.2
0.3 -> 10,808,639,105,689,190 / 36,028,797,018,963,970 < 0.3
their sum is 32,451,855,365,842,673,398,891,542,436,905 / 64,903,710,731,685,348,959,510,906,011,648
= 0.499,999,999,999,999,983,346,654,630,622,653,434,389...
which rounds to 0.5

1 day ago
AutomaticTreat

The limit as x→ username checks out.

1 day ago
Civilchange

Can you do the same for 0.1 + 0.2 please?

22 hours ago
calculus_is_fun
:js::p::bash:

Sure, why not?

0.1 -> 3,602,879,701,896,397 / 36,028,797,018,963,968 > 0.1
0.2 -> 3,602,879,701,896,397 / 18,014,398,509,481,984 > 0.2
their sum is 10,808,639,105,689,191 / 36,028,797,018,963,968 > 0.3
= 0.300,000,000,000,000,016,653,345,369,377,348,106,354...
which rounds to slightly more than 0.3, to everyone's annoyance.

20 hours ago
FirexJkxFire

That doesnt make sense to me. The last 9 in the 0.499999.... and the last 0 in this are at the same location. The furthest along it can round from and result in 0.5 is at the last 9 (rounding the value after it to either 0 or 10). Thusly, for this one 0.3000... the last number where it rounds would either be a 0 or a 1. And surely that 1 would round to 0 instead of 10.

Does it just alwats round up or some shit?

Edit:

Im guessing it has something to do with the binary and them not rounding digits but rather rounding bits. Im just going to assume that under the hood it makes sense.

13 hours ago
Natural_Builder_3170
:cp:

yes its something to do with binary, the number stored must be representable as a binary number (why 0.5 works) look up ieee754 for more info

8 hours ago
your_best_1

https://www.log2base2.com/storage/how-float-values-are-stored-in-memory.html

https://www.h-schmidt.net/FloatConverter/IEEE754.html

Now you can solve all of them

1 hour ago
Extension_Option_122

From my understanding of floating point numbers 0.5 can be exactly represented (basically 1.0-2) so if the numbers used to add to 0.5 have the 'right' amount of error they would result in exactly 0.5.

9 hours ago
RiceBroad4552
:s:

OK, I see, I can post this every day as there are always some people who never heard of IEEE 754 so far:

https://floating-point-gui.de/

1 day ago
brainpostman
:js: :ts: :j: :cs:

At least this time they're not blaming JavaScript for it.

1 day ago
BeautifulCuriousLiar

yeah i hate that, it's obviously typescript's fault

1 day ago
deanrihpee
:cp::cs::gd::rust::ts::unity:

nah man, it's the v8's fault

1 day ago
extremehogcranker

I dunno mine goes really fast but it's expensive to fill up in this economy.

23 hours ago
uvero
:s::j::cs::ts::py:

Or see https://0.30000000000000004.com/

1 day ago
blink012

came here to say this

13 hours ago
NikplaysgamesYT

At my internship I’m currently working on stuff related to IEEE 754 floating point (working on a CPU, but I’m keeping what exactly I do intentionally vague), this stuff is a pain :(

Unfortunately, there isn’t a good way to represent many numbers, such as 0.3, in floating point. As a hardware engineer, we kinda know that floating point isn’t optimal, but it’s the best we’ve got. Even still, floating point is insanely annoying and expensive in hardware

18 hours ago
Christosconst

Can you design a CPU that stores the exponent in one 32 or 64 bit register, and the mantissa in a different register, and somehow associate the two registers for calculations etc?

16 hours ago
Individual-Praline20

I’ve seen junior devs stumped by this, but this is very very basic CS, wtf Literally the things you learn in first classes ffs What tf are they teaching now, how to plug a computer into a power outlet?!

1 day ago
MortifiedCoal
:py:

I just went through an intro java course last year. 0.1 + 0.2 was the example used for why floats / doubles shouldn't be used if you want 100% accuracy. I think 0.2 + 0.3 was the example used before to give an example of floating point numbers adding correctly too. I think I've goten that same demonstration in 3 / 4 different languages I've taken classes for, but the last one was an advanced course with one of the other languages as a prerequisite. I can't 100% blame the jr devs though, most of the classes I've taken from about 3rd grade to my senior year of college were more focused on us passing the tests instead of actually learning the information. I've only had one professor that didn't have final tests, they had final projects that made us actually apply what we learned all year.

Somewhat unrelated, but why this happens wasn't touched on at all in the 6 different programming specific classes I've taken except for my computer architecture and assembly class that isn't part of the CS degree. I accepted that it did happen, but no one explained why besides saying "it just does" until being taught how floating point numbers were converted into binary representations.

1 day ago
SAI_Peregrinus

Or in elementary school. Write the decimal expansion of 1/3. Oops, wrong answer, it's off by a tiny bit no matter where you stop. HumanScript is so silly, right guys!

23 hours ago
nickcash

pfft 0.3̅

21 hours ago
ih-shah-may-ehl

Sure. And in shop class they teach you how a transmission works but the very first time you are asked to disassemble and reassemble one, you will probably have parts left or fail to put something back in the correct location.

Do you remember every single little thing you were taught in school while other information was also injected into your brain with a firehose?

16 hours ago
NewPhoneNewSubs

I mean, meme kinda checks out here. You look at it. It's weird. You do some math to figure it out.

21 hours ago
iismitch55

Mantissa? I hardly know ‘er!

1 day ago
NovaStorm93

https://xkcd.com/217

1 day ago
bassguyseabass
:cp:

some numbers in a base 10 system are non repeating but in base 2 they are repeating.

For example 1/10 in binary is 0.0001100110011001100… repeating. Computers can’t store decimal cause the hardware registers store binary “bits”, not decimal “digits”.

In higher base systems, things that are repeating in decimal are not repeating… for example 1/3 = 0.333333… in base 10. But it is 0.4 in base 12.

1 day ago
calculus_is_fun
:js::p::bash:

It is just different bases have a different set of numbers whose inverse are recurring, base 17 for example is particularly garbage, and has been nicknamed "suboptimal"

1 day ago
DuploJamaal

Isn't any prime base garbage?

We use base 2, because On/Off is simple for transistors, but without that limitation any anti-prime base would be much better.

1 day ago
game_difficulty

Prime bases are bad when it comes to whole number ratios (and a few other random division-related things). Otherwise, they're basically the same

Now, let's talk non-integer bases

22 hours ago
plumarr

Edit : I brain farted and said something silly.

1 day ago
0_P_
:cp::c::py::rust::j::bash:

The rational numbers aren't defined that way. They are any number that can be expressed as "p/q" for integers p and q.

0.1 can always be written as 1 / 10 no matter what base you represent the numbers with

1 day ago
plumarr

Yeah, I stumbled on this one. Totally forgot non repeating numbers :|

1 day ago
deljaroo

they're talking about repeating infinite (rational) and terminating (also rational) numbers only here

1 day ago
No_Hovercraft_2643

some numbers in a base 10 system are non repeating but in base 2 they are repeating.

please name one number, that is repeating in base 2, but not in base 10. i am sure there are none, because 2 is a factor of 10. 5 is not a factor of 2.

1 day ago
bassguyseabass
:cp:

0.1, as mentioned. Base 10 non repeating base 2 repeating, not the other way around.

1 day ago
HeavyCaffeinate
:lua:

Well

Decimal -> Binary

0.5 -> 0.1

0.3 -> 0.01001100110011001100110011001100110011001100110011001100110011...

1 day ago
Splatpope
:c::cp::py::lua::bash:

I don't think this is how floating point numbers are encoded

11 hours ago
ikonet
:j::c:

Good way to weed out jr devs who suggest making money calculations using floats.

1 day ago
LysergioXandex

What is the solution? Do everything with integers (cents) and add the decimal at the end of all calculations?

22 hours ago
ikonet
:j::c:

That’s one solution yes. Lowest common unit, probably fractional cents. The currency separator is a human display responsibility. Can’t trust an approximate calculation (floats) to deal with cumulative tax rates.

20 hours ago
FrenchFigaro
:j::ts::bash:

Or used fixed point arithmetics instead of floating.

It comes with its own set of issues, but imprecision generally isn't one of them.

Java uses BigDecimal as part of its standard library. Other languages have equivalent, either as part of the standard library or as third party ones.

16 hours ago
NikplaysgamesYT

I’m currently doing a hardware internship, where I’m doing stuff related to IEEE 754 as part of designing a CPU.

It’s really interesting to see how for software designers, all they need to know is “floats aren’t perfectly precise”, but when you’re designing the hardware for them, you see why exactly they aren’t precise. Floats are extremely difficult in hardware, and the IEEE 754 standard is the best we’ve got, but not optimal by any means

18 hours ago
Tutul_
:asm::c::cp::j::rust:

What I love about that standard is that it make computation somewhat fast in hardware. And, if I recall, work out of the box for carry values around the point.

Where decimal object with only integers parts doesn't have precision errors but require extra calculations to balance what need to be carried.

And all of that because it's based on fractional base 2. That's the main reason why some base 10 number doesn't exist..

15 hours ago
OldBob10

Tell me you don’t understand floating point numbers without telling me you don’t understand floating point numbers. Also What Every Computer Scientist Should Know About Floating Point Numbers

1 day ago
GiganticIrony
:cp::c::asm:

Only for 64 and 80 bit floats, not 32 or 16

1 day ago
GreatScottGatsby

If arbitrary precision is truly necessary then why not go with something like big decimal? Yes it is slow and yes it takes up a lot of space but it will be precise.

1 day ago
KazDragon
:cp:

Fixed point won't help you.

17 hours ago
savevidio

When you keep adding and subtracting 0.1 at equal but random rates, and the result gradually changes

1 day ago
kbielefe

It's actually not exactly 0.5 either. There's still only 64 bits of precision. You just trust zeroes on the end more.

1 day ago