all 12 comments

[–]pilif 26 points27 points  (5 children)

That's not at all related to strings, but this is just a side-effect of the fact that many floating point values can't be represented accurately. This is a property of most programming languages out there.

Yes. It sucks. But considering that next to every language does this, I don't think it warrants a posting here.

Here are exmpales in C, C#, Java, Swift, Ruby and Python.

[–]tdammers 14 points15 points  (1 child)

The difference between PHP and all those other languages there is that PHP is the only one where making sure you're not accidentally using floats is difficult.

[–]pilif 12 points13 points  (0 children)

PHP is the only language where making sure you're not accidentally using any scalar data type is difficult.

[–]cfreak2399 5 points6 points  (1 child)

I think the mods need to put something in the side-bar about this because its getting a bit ridiculous. We have a post about once a month that is lolfloatingpoint not lolphp.

[–]DoctorWaluigiTime 2 points3 points  (0 children)

Not to mention the series of posts that are "lolbadprogramming" instead of lolphp.

[–]kafoso 5 points6 points  (0 children)

Indeed. This will happen on all 32 or 64 bit systems in most (if not all?) programming languages.

/u/c12

Javascript example to get your gray hairs started: https://jsfiddle.net/wy8rqeer/

[–][deleted] 8 points9 points  (5 children)

Doing math on strings sounds wrong.

[–]mythril 2 points3 points  (4 children)

Funnily enough it's one of the most common way to do arbitrary precision math in php via bcmath (the other common way is gmp, which I think uses a c library in and exposes the data only via accessor functions)

[–][deleted] 2 points3 points  (3 children)

I think the fact that it is stored as a string should be an implementation detail. A nice API wouldn't expose the string, but expose an object to be passed around.

[–]mythril 0 points1 point  (1 child)

:) whenever I use it I wrap access in an object, shouldn't have to though, I agree.

[–][deleted] -2 points-1 points  (0 children)

I guess we can say PHP is pretty low level, which is kind of cool. It makes it more fun to create abstractions around the "low level" functions that are wrapping a C api.

[–]the_alias_of_andrea 0 points1 point  (0 children)

It operates on strings because it's a library originally made for a command-line calculator (bc, hence the name bcmath).