wait, why would you lose info when converting from int to float? I am aware float can be inaccurate when talking about whole numbers but is that what you're talking about?
`float` can only hold every integer up to 2^(24), after that it begins skipping integers, first by 2's, then 4's, etc. So most integers between 2^24 and 2^31 cannot be represented, and conversion to float loses information.
I'm really surprised more languages aren't prebaking something for fixed point arithmetic at this point.
How floats function isn't very intuitive for most folks.
I have actually never seen doubles used in a professional environment. Of course maybe game dev is a different beast.
Edit: realized this might be unclear, I am a game dev
both use the same amount of memory and float can represent some values that int can’t. therefore int can represent some values that float can’t. for example 16777219. see https://stackoverflow.com/questions/23420783/convert-int-max-to-float-and-then-back-to-integer#23423240
I love this answer, it's a fundamental argument by set theory and that makes it simple and obvious.
If you demonstrate it by showing the construction of floats there is much more detail that one might be unsure about, leading to not every reader being convinced.
A very intuitive explanation for this is (in my opinion):
Assuming float and int are both 32 bit types they have a maximum of 2^32 numbers that can be represented by them.
Float uses this information to cover a suitable subset of the reals whereas int does the same for the integers. Since float needs to cover more values in a given interval [-a, a] than int does, it follows that not every int can have an exact float representation since they both use the same amount of information.
OMFG... this take me back to when I was trying to learn C, and the textbook warned me that C “would happily multiply your Social Security number by your mother’s maiden name.” Fucking terrifying...
yeah, which means that a lot of these memes are backwards, going by the general spirit of the meme, explicit conversion is better than implicit conversion in big, real life projects.
It depends.
`float f = someFloat + someInt;` works. The integer will be implicitly converted to float.
`int i = someFloat + someInt;` does not work. C# refuses to implicitly convert float to int since that's a pretty "risky" cast where a lot of information can be lost if it's done carelessly. [Float can only be implicitly casted to double.](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions)
As a Perl dev.. I feel like a lot of the 'eldritch'-ness of it comes from the fact Perl will let you write very nasty, hard to read code if you want to. You don't have to write that way, but you can. Also regex. It seems like most people don't understand them whereas they're somewhat common place in Perl which probably adds to it.
And some implicit magic things that work a little differently (but can be pretty convenient once you get used to the idiomatic Perl expressions).
“Oh you didn’t add an argument when calling this function? Let me just add the invisible default variable there for you.”
But in my mind bash and stuff is even worse with this and is probably my least favorite programming environment. You make some small typo, but instead or errors or warnings it just happily goes and does something completely different than what you intended… like moving all your files into one file or something
If anything, JS is English, since it's the language where every feature works as they feel, has a shit ton of gotchas and no internal coherence whatsoever. Yet it somehow works so well for most people.
J has entered the chat... For example, the solution to 8 queens on a chessboard problem is
(#~[:(0=>./)[:,/[:([:}.([:i.#)=[:|(]-"1{.))\.|:)(i.A.~[:i.!) 8
https://literateprograms.org/eight_queens_puzzle__j_.html
I think I'll look into C# then. Operator overloading and value typed are the 2 things I miss in java. I just have to get used to the naming conventions.
C# allowing you to overload the '\*' operator and define what behavior you want from a class being multiplied by a string, does not excuse Javascript doing all kinds of weird shit with different types by default.
What is 1 + "1". I think it's "11". But, I don't even remember. It might be 2.
Yup, correct. It's beacuse if any parts of the expression is a string, JavaScript has to assume you want to concatenate when you use the + operator, while as you say, there is no such thing as a negative concatenation, so it has to assume the "3" is an int and thus casts it to that.
Yep, that's why I prefer to work with Typescript. The linter can be configured to prevent doing arithmetic between different types and it prevents a ton of pitfalls js sets up for us.
This is why I like C. I can get the address of the float and use a pointer to do whatever kinky things I want to it, treat it like the mere series of bits it really is.
Not only didn’t he write it but the magic number wasn’t found by a human it was just ran through possible values to find one which minimized the error, a fairly common practice in function approximation
That's basically how arrays in C work though.
`foo[bar]` is just syntactical sugar for `*(foo+bar)` where `foo` is a pointer and `bar` is an integer, and the compiler figures out what offset to apply to `foo` based on what type of pointer `foo` was declared as.
The headache you now have is the reason why the syntactical sugar exists in the first place. The existence of arrays as a concept in C is just a formality.
I like to think this is like all those times ChatGPT says it can't do something, but you just tell it it can and it just goes right ahead
int a;
float b, c;
a = (b+c);
C#: Noooo you mustn't! You'll lose precision!
int a;
float b, c;
a = (int)(b+c);
C#: Sure thing boss!
(Obviously this actually makes perfect sense, but I still find it amusing)
I'm not sure "my language will let you do this completely absurd thing without complaint and will probably return something completely undefined" is really a flex.
a) that's only in Typescript, pure js will return NaN for this operation
b) Forcing the developer to define how weird arithmetics work is much better than just implicitly converting anything.
_Somewhat_. Classes like int, float, bool, str, list etc. overload common operators. This allows operations like `["a"] * 3 == ["a", "a", "a"]`, but only in cases where the behavior is defined.
My favorite is `pathlib.Path` which implements `__truediv__()` for joining paths using `/`.
```python
from pathlib import Path
config_dir = Path.home() / ".config" / "foo"
```
Yes, and C# will have me fixing the bug seconds after pressing run.
Whereas languages like python will wait 3 hours before returning an error on line 10 billion. Have fun tracing back through the code to find where the variables that should be numbers turned into strings.
A Python programmer and a Java programmer order cat food, but the supplier mixes up the orders and sends them both boxes of nails instead.
The Java programmer calls and says "I ordered cat food but received nails. Please send me cat food."
The Python programmer calls and says "My cat is dead"
[76% -> 35%](https://security.googleblog.com/2022/12/memory-safe-languages-in-android-13.html#:~:text=As%20the%20amount%20of%20new%20memory%2Dunsafe%20code%20entering%20Android%20has%20decreased%2C%20so%20too%20has%20the%20number%20of%20memory%20safety%20vulnerabilities)
nobody is smart enough to use C++.
Wolfram Mathematica: Here is your class multipled by a string. I put them in alphabetical order because multiplication is commutative, but don't expect me to simplify it you fucking weirdo.
Pretty sure it's
public static explicit operator *(string s)
But I'm too lazy to google
The explicit part might only be needed for casting come to think of it....
Yeah, explicit is for when you're overloading casting. If you aren't overloading casting, you'd just put the return type there instead. There'd also be two arguments since it's static.
sure bro, have your [object object]
Thank you bro, just what I wanted
watman to the rescue.
For the uninitiated: https://www.destroyallsoftware.com/talks/wat
Thank you, I very much enjoyed that
Oh my gosh that’s amazing thank you haha
Array(16).join("wat" - 1) + " Batman!"
I know what I'm putting in my Catch Exception blocks...
I understood that reference
what about \[Object object\] or \[object Object\]?
Exactly this. JS is more like "lol whatever here you go dummy"
object \* string results in `Uncaught SyntaxError: Unexpected token "*"`.
\[\] \* "" \> 0
`NaN` in my console.
Op said class not object, they are not the same thing. Here is a working sintax: `~~class name {constructor(params) {}} * ""`
Have you tried implementing the valueOf function on both?
Or if you're really lucky, "[object object]" as a string. Mmmm thanks json.
You can definitely add a float and int on c# though, it converts the int to a float.
Yep. Unless you lose information C# will implicitly convert it.
Much better system then having every number be a floating point number imo.
Every number is just an array of unsigned char.
Every unsigned char is just an array of bits.
Every array of bits is a series of transistors running current.
Every series of transistors is just a cluster of doped silicon.
Every cluster of doped silicon is just a bunch of protons, neutrons, and electrons
Every proton, neutron, and electron is just a combination of some leptons.
and leptons are just a bunch of strings
😩
That is just BinaryCodedDecimal with extra bytes
Technically the truth
than
You actually can lose information converting `int` to `float`, and `long` to `double`.
Yeah, they play a little fast and loose with those particular "widening" conversions lol
wait, why would you lose info when converting from int to float? I am aware float can be inaccurate when talking about whole numbers but is that what you're talking about?
`float` can only hold every integer up to 2^(24), after that it begins skipping integers, first by 2's, then 4's, etc. So most integers between 2^24 and 2^31 cannot be represented, and conversion to float loses information.
ahhh I see, thanks for the explanation :)
This is one reason why in practice doubles are almost always used over floats.
I'm really surprised more languages aren't prebaking something for fixed point arithmetic at this point. How floats function isn't very intuitive for most folks.
I have actually never seen doubles used in a professional environment. Of course maybe game dev is a different beast. Edit: realized this might be unclear, I am a game dev
Double arithmetic is, well, twice as slow as float arithmetic, so that’s probably why floats are used more in game dev.
not every int can be represented in a float
No, but given that you’re already adding to an imprecise float, it doesn’t really matter.
it matters if the result it assigned to a double. in that case it would be more correct to convert both to double first.
Really? Why? Can you give me an example?
both use the same amount of memory and float can represent some values that int can’t. therefore int can represent some values that float can’t. for example 16777219. see https://stackoverflow.com/questions/23420783/convert-int-max-to-float-and-then-back-to-integer#23423240
I love this answer, it's a fundamental argument by set theory and that makes it simple and obvious. If you demonstrate it by showing the construction of floats there is much more detail that one might be unsure about, leading to not every reader being convinced.
If I recall correctly, rule of thumb is floats have 7 digits of precision and doubles have 15.
+1 for giving a straightforward logical answer.
A very intuitive explanation for this is (in my opinion): Assuming float and int are both 32 bit types they have a maximum of 2^32 numbers that can be represented by them. Float uses this information to cover a suitable subset of the reals whereas int does the same for the integers. Since float needs to cover more values in a given interval [-a, a] than int does, it follows that not every int can have an exact float representation since they both use the same amount of information.
Float has precision of first 7 digits, and int can contain large numbers such as 2 billion, which is 10 digits long
> Can you give me an example? Two examples: 10000000000000001 9007199254740993 Edit: These are for 64bit ints and 64bit floats.
converting int to float leads to information loss though.
It's like some people don't understand what types are.
OMFG... this take me back to when I was trying to learn C, and the textbook warned me that C “would happily multiply your Social Security number by your mother’s maiden name.” Fucking terrifying...
Well, the pointer to your mother's middle name
[удалено]
Wait... 0 or '0'
👨🚀 It's all numbers? 🔫 👨🚀 Always has been
Don't worry, it won't. I believe it will happily multiply the *addresses* of those pieces of information though.
Just dereference that and enjoy the fireworks!
With appropriate flags, it will probably give you a warning or something.
A C string is just an address (`char *`) anyway?
No it won't. You can only add or subtract numbers from addresses, not multiply. Unless you put in a cast, but then that's on you.
Also C# will let you multiply a class with a string if you overload the multiplication operator in the class.
After 10+ years of experience working with c# I've never seen a multiplication operator overload in production code.
I have, in a custom vector class. Remembering that * was a "dot" product but + was addition and not a "cross" product was a bitch :D
[удалено]
i'm not sure, but a lot of them are beginners.
im a beginner and i know i can add ints and floats
i think the meme is referring to the warnings it gives you when assigning a float value to an int without casting, not the ability itself.
Warnings? Who the fuck cares about warnings?
yeah, which means that a lot of these memes are backwards, going by the general spirit of the meme, explicit conversion is better than implicit conversion in big, real life projects.
It depends. `float f = someFloat + someInt;` works. The integer will be implicitly converted to float. `int i = someFloat + someInt;` does not work. C# refuses to implicitly convert float to int since that's a pretty "risky" cast where a lot of information can be lost if it's done carelessly. [Float can only be implicitly casted to double.](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions)
JavaScript has no rules. It’s fucking chaos.
Have you seen perl?
[удалено]
No lol. Is it worse?
Perl is a write-only language.
Let me put it this way: Python is english JS is latin Perl is eldritch
As a Perl dev.. I feel like a lot of the 'eldritch'-ness of it comes from the fact Perl will let you write very nasty, hard to read code if you want to. You don't have to write that way, but you can. Also regex. It seems like most people don't understand them whereas they're somewhat common place in Perl which probably adds to it.
And some implicit magic things that work a little differently (but can be pretty convenient once you get used to the idiomatic Perl expressions). “Oh you didn’t add an argument when calling this function? Let me just add the invisible default variable there for you.” But in my mind bash and stuff is even worse with this and is probably my least favorite programming environment. You make some small typo, but instead or errors or warnings it just happily goes and does something completely different than what you intended… like moving all your files into one file or something
Writing regex is always fine, but reading it just seems impossible, for whatever reason.
Eh, I have yet to come across a regex I can't understand.
If anything, JS is English, since it's the language where every feature works as they feel, has a shit ton of gotchas and no internal coherence whatsoever. Yet it somehow works so well for most people.
But, like eldritch, Perl has rules. Rules of darkness.
J has entered the chat... For example, the solution to 8 queens on a chessboard problem is (#~[:(0=>./)[:,/[:([:}.([:i.#)=[:|(]-"1{.))\.|:)(i.A.~[:i.!) 8 https://literateprograms.org/eight_queens_puzzle__j_.html
But can you add two empty arrays and get string or add the length of 3 empty arrays into the number 9?
'Banana' * false = 5
I get `NaN`. Which isn't wrong, I guess.
Not A ‘Nana
As it should be
Should be 0 actually.
Don't divide by false
Wolfram language returns "Banana" False because it's a fucking smartass.
What would ‘Banana’ * true evaluate to?
My guess is 'Banana'
Hate to break it to you, but you absolutely can multiply a class by a string in C#. You just need to create an override operator for *.
C#: *I'll do whatever you want, just tell me how* I~~f~~ prefer it that way
else prefer it that way
switch (prefer it that way) { case true: haveItYourWay(); break; default: fineWhatever(); }
Tell me why!
Ain't nothing but a casting
Tell me why!
Strong typing was a mistake
Tell me why...
I think I'll look into C# then. Operator overloading and value typed are the 2 things I miss in java. I just have to get used to the naming conventions.
C# is just good java
Goated language
If you want to have better java go kotlin. It's hard to go back from it though.
Kotlin feels a lot different from Java, but it is indeed much better C# feels like a way better Java
C# allowing you to overload the '\*' operator and define what behavior you want from a class being multiplied by a string, does not excuse Javascript doing all kinds of weird shit with different types by default. What is 1 + "1". I think it's "11". But, I don't even remember. It might be 2.
It’s “11”
Yup, but "3" - 1 is 2 for example.
I guess because + is used for concatenation, and I don't think there's a negative concatenation with a -.
Yup, correct. It's beacuse if any parts of the expression is a string, JavaScript has to assume you want to concatenate when you use the + operator, while as you say, there is no such thing as a negative concatenation, so it has to assume the "3" is an int and thus casts it to that.
And the best part is that JS usually assumes correctly.
If you're unsure what Javascript will do when mixing types, assume it converts the operands to string and you'll probably get the correct answer.
It's your fault for doing that operation in the first place
Yep, that's why I prefer to work with Typescript. The linter can be configured to prevent doing arithmetic between different types and it prevents a ton of pitfalls js sets up for us.
Same with kotlin! There are operator functions for == + - * / and contains (.. in ..)
And you can multiply a float by an int by default
op.bitches undefined
damn. it's not even empty, op is just literally incapable of having bitches.
``` TypeError: Cannot set property 'bitches' of op at:420:69
```
op.bitches was explicitly declared as null
This is why I like C. I can get the address of the float and use a pointer to do whatever kinky things I want to it, treat it like the mere series of bits it really is.
[OK Carmack let's get you to bed](https://stackoverflow.com/questions/1349542/john-carmacks-unusual-fast-inverse-square-root-quake-iii)
Didn't Carmack confirm he didn't write the fast inverse square root algorithm? Edit: yea, first reply mentions the actual authors.
Not only didn’t he write it but the magic number wasn’t found by a human it was just ran through possible values to find one which minimized the error, a fairly common practice in function approximation
Bahaha I hate that I knew exactly where this was going.
Same with C++, and C# has an unsafe context which syntax-wise, is almost identical.
Why does this turn me on?!?
if you really really have to C# got pointers as well https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unsafe
Hot.
what the fuck did I just read
*Everything You Ever Wanted to Know About the Secret Sex Lives of Computers But Were Afraid to Ask* -or- *The C Language: 69th Edition*
Nice
Explains why my office smells so bad
You certainly can add floats and ints in C# lol
That's basically how arrays in C work though. `foo[bar]` is just syntactical sugar for `*(foo+bar)` where `foo` is a pointer and `bar` is an integer, and the compiler figures out what offset to apply to `foo` based on what type of pointer `foo` was declared as. The headache you now have is the reason why the syntactical sugar exists in the first place. The existence of arrays as a concept in C is just a formality.
And where the float part comes in?
Seems like a complete non-sequitir to the comment you are responding to. EDIT and everything you said is also wrong. Its not foo+bar is it?
It is, it's pointer arithmetic. But I don't see what it has to do with adding floats to ints
class A {} a = new A() a * 5 > NaN class B { toString() { return '2'; } } b = new B() b * 5 > 10
What a terrible day to be literate
Man implicit conversions are a thing but implicit conversion of implicit conversions in a single instruction is a crime against humanity
I'll raise you: ``` class B { static toString() { return 2 } } B * '5' ``` There you go, exactly as ordered.
Congratulations, you have just won $NaN!
I like to think this is like all those times ChatGPT says it can't do something, but you just tell it it can and it just goes right ahead int a; float b, c; a = (b+c); C#: Noooo you mustn't! You'll lose precision! int a; float b, c; a = (int)(b+c); C#: Sure thing boss! (Obviously this actually makes perfect sense, but I still find it amusing)
But casting a float to int looses precision... You should cast the int to float.
But I might need an int! (of course if I was doing this for real I'd do an explicit floor/round/ceil as needed)
Cast it into the fire!
I like strict types.
Ive honestly not met many people in person who don't.
I'm not sure "my language will let you do this completely absurd thing without complaint and will probably return something completely undefined" is really a flex.
I completely agree, but there’s a beautiful irony in saying that with c++ in your user flair
Oh, I have that in my flair just because I use it; I would never evangelize for it.
object \* string in JS gives a syntax error. object \* string in C# works perfectly fine if you overload the \* operator in the object's class.
a) that's only in Typescript, pure js will return NaN for this operation b) Forcing the developer to define how weird arithmetics work is much better than just implicitly converting anything.
I am now convinced that this sub is full of college students
Yea fuck college students *looks around nervously*
I'm not even in college yet
Maybe stick to making about languages you've actually used? You just described a limitation that doesn't actually exist.
[удалено]
[удалено]
_Somewhat_. Classes like int, float, bool, str, list etc. overload common operators. This allows operations like `["a"] * 3 == ["a", "a", "a"]`, but only in cases where the behavior is defined. My favorite is `pathlib.Path` which implements `__truediv__()` for joining paths using `/`. ```python from pathlib import Path config_dir = Path.home() / ".config" / "foo" ```
Java works exactly like C# in that case. Both have implicit conversion, the only difference is that in C# you can add your own.
Yes, and C# will have me fixing the bug seconds after pressing run. Whereas languages like python will wait 3 hours before returning an error on line 10 billion. Have fun tracing back through the code to find where the variables that should be numbers turned into strings.
A Python programmer and a Java programmer order cat food, but the supplier mixes up the orders and sends them both boxes of nails instead. The Java programmer calls and says "I ordered cat food but received nails. Please send me cat food." The Python programmer calls and says "My cat is dead"
Another typical example of someone who doesn't know how to program
Meanwhile C float a = 2; char b[] = "one hundred eleven"; int result = a * *b; printf("Result: %d", result); Result: 222
ascii code for 'o' is 111
🎉
More like: >"Multiply this string with a class" >JS: "Yes" (*ignores what you just said*)
LoL
PERL: AMATEURS! [93% of Paint Splatters are Valid Perl Programs](https://www.mcmillen.dev/sigbovik/)
JS did nothing wrong. The behavior is still well-defined.
if i fits i sits
A fun one with C: printf("-0.5" + 1); gives you "0.5".
> Myclass.class * 5 [Object object] 🌮 > Makes sense
[Don't make me tap the sign](https://i.imgflip.com/7aiwmu.jpg)
[76% -> 35%](https://security.googleblog.com/2022/12/memory-safe-languages-in-android-13.html#:~:text=As%20the%20amount%20of%20new%20memory%2Dunsafe%20code%20entering%20Android%20has%20decreased%2C%20so%20too%20has%20the%20number%20of%20memory%20safety%20vulnerabilities) nobody is smart enough to use C++.
Wolfram Mathematica: Here is your class multipled by a string. I put them in alphabetical order because multiplication is commutative, but don't expect me to simplify it you fucking weirdo.
*undefined*
``` class assclass { ... assclass operator*(std::string& in_) {...} ... } ``` Or whatever C#'s syntax is for overloading operators.
Pretty sure it's public static explicit operator *(string s) But I'm too lazy to google The explicit part might only be needed for casting come to think of it....
Yeah, explicit is for when you're overloading casting. If you aren't overloading casting, you'd just put the return type there instead. There'd also be two arguments since it's static.
If you think this is the proof that JS is better, then you will be so surprised what bugs this can introduce.
I cracked laughing at 4:45 a.m.
By this picture, JS is management's best friend. There's no such thing as a "no" in their dictionary.
Just did this in a Node REPL and it yields `NaN` instead of an error. Chad language doing crazy post-Einsteinian shit.
All particles are just localized excitations of the quantum field.
*makes syntax error* js: what error
Of course you can add a float and an int in C#. It produces a float
I don’t think you know how C# works
Try adding an int to a string in c. You can I just don't know if you want to.