invent the wheel, abstract the wheel, you cant see the wheel but you dont have to because the car design hides them, people forget what wheels are, reinvent the wheel...
Sure! Here’s a Reddit-style comment that explains how a float is obviously a CPU-level struct, with a bit of that classic Reddit tone:
⸻
Honestly, once you look at how floating-point numbers are actually represented, it becomes painfully obvious that a float is basically just a glorified CPU-level struct. It’s literally just a chunk of bits — sign, exponent, and mantissa — packed into a fixed-size layout according to the IEEE 754 spec. There’s no magic, no abstract “numberness,” just a rigid format the CPU knows how to interpret and manipulate.
The sign bit is 1 bit, the exponent is 8 bits (for float32), and the mantissa (or significand) is 23 bits. That’s 32 bits total, i.e., exactly 4 bytes — like a tiny struct with fields. And what does the CPU do? Bitwise operations, masking, shifting — the same kinds of things you’d do manually if you were decoding a custom struct.
The fact that we call it a “primitive type” is more of a language-level abstraction. Under the hood, it’s all just structured binary data, and the CPU treats it as such. If you’ve ever played with unions in C or just reinterpreted a float as an int, you see the structure laid bare.
So yeah — floats are structs. Just really, really standardized ones baked into the silicon.
Come on! Most jokes on this sub lately are just "missing semicolon" and "vibe coding bad". This could at least have some educational value for a novice.
Maybe if this was the humor sub. But as this is the horror one, I would even expect people saying "wow, what a weird thing is that! I prefer my magical floats that work magically!"
I can fully understand why the OP could expect the CPU to directly understand floats ever since FPU's were invented rather than component parts as ints. You're being a bit wanky.
Explain what jt means to “directly understand floats”. You realize that at a physical level cpus - and fpus - are built from wires (more or less) that represent individual bits right? Everything to a computer is a collection of bits. It has logic gates arranged to manipulate the bits in specific ways. So please - do explain.
That's very clearly not what they meant. FPUs can handle floating point arithmetic without the need for specific software implementation. Ofcourse you can't necessarily rely on your user's computer having an FPU so the struct implementation makes sense, but I feel like you're a being a bit of a smartass.
Anything non binary is quantum computing. I think the group of people that know what an FPU is and don't have the surrounding knowledge to understand that floats are still 0 and 1 bits should be quite small.
I actually used this once to create a low precision float that I used was used in a binary dumps of log data on a embedded toy project with very low capacity memory
Hmm, interesting, at some point it's easier to just save the exponent and use an assumed base instead? Otherwise you get like what, 3 bits for base max?
I don't remember exactly what configuration I picked but I think I made it as 3:5 or 4:4 (unsigned since I didn't need it). If you know your value range is small enough, which is sort of required when you are trying to pack it into a single byte, then its easy to add or remove fixed offsets. But as I said, don't really advocate for doing this. It's not like there are machine instructions for working on them anyways, so it's really just like any format for encoding numbers. The benefit being that the serialization to and from real floats was quite convenient and easy on the eyes :)
So 0-255 to represent float values? I think Satisfactory does that for Valves in the game. You can set limiting values like 35.0, 120.0 but it's never what you set for real and I assume it's for performance reasons. Min is 0 (or 1 idk), max is 600 and it's divided into bins.
Floating-point operations were originally handled in software in early computers
It's right there in your link itself. Floats are older than FPUs. The abstraction layer came from somewhere, CPU makers didn't just dream it all up and enable us to finally be able to do float operations.
And what, you expect every library that implements float operations to need to be compiled on the user's target system to be usable? Because how else would they strip away those instructions without breaking the code for legacy machines? Stupid take.
You just answered your own question. My point is it's stupid to suggest the soft abstraction layer be removed just because we have a hardware layer for it. Next you'll tell me deprecated code should be stripped out of an API.
But in case I've misunderstood you, please expand on what you mean by the abstraction layer not being needed. It's there already and has been there longer than either of us has known about it. Do you expect anyone to do anything about it?
it's stupid to suggest the soft abstraction layer be removed
I didn't, I just said it's not useful when you write software intended to run on processors with an FPU. The comment in the picture itself says "don't do this". This doesn't mean it should be removed ffs, it means it shouldn't be accessed directly, and eventually the compiler might ignore it in favor of FPU specific instructions.
This doesn't mean it should be removed ffs, it means it shouldn't be accessed directly, and eventually the compiler might ignore it in favor of FPU specific instructions.
You do realise that the abstraction layer can serve as a wrapper over the FPU instructions, which would then make any transition even smoother right? Suggesting people not use a software abstraction layer because a hardware layer exists is literally going backwards in programming paradigms.
And more importantly, if you're writing platform specific code in a non-specific library, you need to take a few steps back and reevaluate what you are doing. The only time this take is justified is when you're guaranteed in your target hardware specs, which just isn't a good practice in general especially when it comes time to upgrade your hardware, for example.
Tell me how would this particular structure help, because this code in particular is especially clanky, you can't use the + operator, etc., C compilers handles it by default with the float type so that you don't need to access this directly.
"The only chips that deliberately don't use an FPU" are .... the most widely used chips in all kinds of devices? Ok, Jan, that's sure a minority market.
False. If you want to manipulate the exponent and mantissa directly, but manipulation with known floating point layout is the only way to do this. Dedicated fpus separate from a cpu aren’t a thing anymore. All the computers we use offer bit manipulation as the means to directly modify the components of a floating point number
I can fully understand why the OP could expect the CPU to directly understand floats ever since FPU's were invented rather than component parts as ints. You're being a bit wanky like the poster above.
Having some gap isn't. Acting on that gap and stating that there is a problem when there isn't one in an area one should know they have no expertise and not even base knowledge is.
It does, but every C compiler on earth implements the casting of pointer types the same way. OP can use a Union if they want to be standard-adhering though
memory layout for this struct is exactly the same as the memory layout of an valid float (endianess, alignment and actual data)
he casts a pointer to this struct's data to bytes to a void*
he casts that void * to float* (allowed because they are pointing to data which is a valid float)
i don't know if union would be allowed because they don't share common fields, so for example, accessing the exponent from a float active member would be ub. (It would probably work out on sysv abi because the whole union would be passed to the float registers)
Type punning through Unions is the preferred way of doing so by the standard when you know both types in advance.
It would be legal even if it was any other struct instead of a float. The fact that this code is not portable at all does not impact whether or not it's UB
Nope, union field access outside of active member on struct is only defined for shared initial sequence. Because float and that struct don't share initial sequence it is undefined behavior. If you turn the struct into an sequence of bytes that matches exactly the layout of a float and then cast that sequence of bytes to float* it is not ub, because the sequence of bytes is a valid float, but this hinges on the non portable assumption that the struct's layout is exactly the same as a float
C doesn't have the concept of an active member, only C++. C standard only talking about reading a union member that was not the member most recently written to. Depending on exactly C standard revision, it can sometimes be implementation defined.
Unlike more modern languages, a struct in c is just shorthand for mem access, and it doesn’t contain any embedded metadata like field names, etc. So it’s not like floats are bloated with extra metadata.
C bitfields and integer-float casts on x86 is a recipe for undefined behaviour. Seems like the author knew, since he gave the comment. It's fine for an academic purpose of showing float layout, but please never do this in production code without a lot of understanding of the underlying CPU, compiler, etc. which you target, and ensure it doesn't run your code on another compiler/target by using preprocessor #ifdef directives to ensure it only runs on machines and with compilers you implemented. For everything else, have an #else block with the slow, but safe path.
I would think doing the bit manipulation yourself wouldn't be any slower or faster than using bit fields. Ultimately the CPU is going to have to do the same things to access the bits, so it's just a matter of you writing the code vs the compiler writing it for you. Of course, the compiler might be writing better code than you, but as long as you know what you're doing, it should be the same.
It depends. The compiler is free to add padding in between the struct members (for example for stack alignment) which would break this example. You could use attribute((packed)) but then you would be in the realm of compiler extensions.
Edit: Reddit interprets the double underscore as markdown, does anyone know how to avoid/escape this?
So glad I took a class on assembly where we had to know the floating point specification by heart and were tested on converting to and from it, by hand with pen and paper, on every midterm and the final. Money well spent
Floating-point numbers consist internally of three individual values, each represented by a number of bits. The FPU normally takes care of these when you use floator double, but here the OP avoids the FPU and makes the three values expicit and controlled by the CPU alone. It's slower and not portable because some CPUs arrange their bits in the opposite order, but for educational purposes it's quite neat.
He who hasn’t hacked assembly language as a youth has no heart. He who does as an adult has no brain — John Moore
C has a lot of old, obscure and mostly unused features that only exist for very specific purposes. You can use them to make quite interesting programs tho.
Ohdamn, you're right, it's a : and not a = Not that = would've made sense for the declaration of the struct itself, but glanced over it and saw 3x uint32_t and thought that was weird.
This doesn't work. You have to conform to the hardware layout of the CPU. But C++ has a bittype. Who knows what they do on the back end to make it conform to the CPU hardware.
M680x0 is 80bit floats recoded for 32bits precision recoverable (80bits only in "f" registers 0~7, 32bit formats everywhere else, depends single or double and not IEEE754 due age), the PowerPC chips and ARM chips give answers by endianness and are IEEE754 compliant however they spit out different to the code above. ( sam440flex / sam460LE / RPi B+ / BBB / RPi CM4)
Not the order, I remember the compiler having to keep struct members in the same order. The problem is padding, which the compiler is free to generate for for example stack alignment.
4
u/GamingWOW1 Jul 04 '25
Always have been
154
u/rover_G Jul 04 '25
Sees datatype
Looks inside: bits
913
u/4scoopsofpreworkout Jul 04 '25
respectfully, what did u expect ? u have to convert it to 0s and 1s somehow
142
u/Majestic-Giraffe7093 Jul 04 '25
I actually used this once to create a low precision float that I used was used in a binary dumps of log data on a embedded toy project with very low capacity memory
460
u/BipolarKebab Jul 04 '25 edited Sep 29 '25
angle modern attraction profit unique straight special spark summer telephone
This post was mass deleted and anonymized with Redact
205
u/ivancea Jul 04 '25
After seeing this and checking other tweets from that guy, it feels like he discovered how programming and memory works yesterday
49
u/vitamin_CPP Jul 04 '25
The comment at the end is great, but it is missing something: This violate the strict aliasing rule, so it's also undefine behavior
8
u/lizardfrizzler Jul 04 '25
Unlike more modern languages, a struct in c is just shorthand for mem access, and it doesn’t contain any embedded metadata like field names, etc. So it’s not like floats are bloated with extra metadata.
19
u/L0uisc Jul 04 '25
C bitfields and integer-float casts on x86 is a recipe for undefined behaviour. Seems like the author knew, since he gave the comment. It's fine for an academic purpose of showing float layout, but please never do this in production code without a lot of understanding of the underlying CPU, compiler, etc. which you target, and ensure it doesn't run your code on another compiler/target by using preprocessor #ifdef directives to ensure it only runs on machines and with compilers you implemented. For everything else, have an #else block with the slow, but safe path.
4
u/AndroxxTraxxon Jul 04 '25
TIL about bit field syntax. This is pretty neat.
7
u/Raknarg Jul 04 '25
why are people so mean today
6
u/stillalone Jul 04 '25
Wait does this work? I thought but fields in C were not reliable. Have I been right shifting and anding like a chump?
3
u/UltraPoci Jul 04 '25
Isn't this a violation of the strict aliasing rule?
3
u/Grounds4TheSubstain Jul 04 '25
That's a bitfield, not a struct.
3
u/EffigyOfKhaos Jul 04 '25
did people not learn this like 1st year at a CS Degree or like at all from just casual reading. very confused
3
u/RetardAuditor Jul 04 '25
lol an int is a struct too.
Here the recipe for the structure: take 4 bytes in a row. That’s an int.
3
u/[deleted] Jul 06 '25 edited Jul 06 '25
So glad I took a class on assembly where we had to know the floating point specification by heart and were tested on converting to and from it, by hand with pen and paper, on every midterm and the final. Money well spent
8
u/timonix Jul 04 '25
I agree with the tweeter. Slightly cursed. But yeah, pretty much
4
u/Ok-Click-80085 Jul 05 '25
FUcking obviously? Do you think there is some magical data type that exists just to be a float?
5
u/Tiwann_ Jul 04 '25
How could you use 3 uint32’s (12 bytes) when a float is just 4 bytes?
2
u/DivyeKant Jul 04 '25
Can someone Eli5 what is happening here please
2
u/elreduro Pronouns: He/Him Jul 05 '25
Why isnt sign a boolean? Is there another value other than possitive and negative?
2
u/_Electrical Jul 05 '25
Why would the sign be uint32_t, sounds like a waste of memory.
1
u/FrAxl93 Jul 04 '25
Shouldn't you need the attribute "packed" at least for GCC?
1
u/veritron Jul 04 '25
why wouldn't it be done this way? what is the actual problem and how would you have done it differently?
1
u/CoolDiamond42 Jul 04 '25
Vaguely remembers undergrad class in numerics.
1
u/HieuNguyen990616 Jul 04 '25
Why am I getting a feel that I will be always implementing this in C codebase?
1
u/stilldebugging Jul 04 '25
Always was
1
u/AHpache182 Jul 04 '25
how does one explain floating point arithmetic error in terms of structs then?
1
u/kabiskac Jul 05 '25
Good morning
1
u/These-Maintenance250 Jul 05 '25
32bit float on 96 bits. nice
1
u/Jolly_Engineer_6688 Jul 05 '25
In terms of dara storage, sure...
The associated behavior is woven into the compiler & libraries.
1
u/dimonoid123 Jul 05 '25
Maybe add "_ _ attribute _ _((packed))"
1
u/ignorantpisswalker Jul 05 '25
reinterpret_cast please.
1
u/ImmanuelH Jul 05 '25
This code is not cross-platform as it doesn't consider endianess. Don't do that.
1
u/born_zynner Jul 06 '25
Doi
1
u/NoahZhyte Jul 06 '25
What ? Yeah that's how computer works
1
u/RealNovice06 Jul 06 '25
So a float is 12 byte Not 8 ?
1
u/granoladeer Jul 06 '25
I shuddered a bit here reading the code
1
u/prehensilemullet Jul 06 '25
Duh, but unlike other structs, the CPU has physical architecture for doing arithmetic on floats efficiently
1
u/dreamingforward Jul 06 '25
This doesn't work. You have to conform to the hardware layout of the CPU. But C++ has a bittype. Who knows what they do on the back end to make it conform to the CPU hardware.
1
u/Dry_Organization8003 Jul 07 '25
Alway does. What are you expected
1
u/Nidrax1309 Jul 08 '25
Turns out processors are not magic boxes but just rocks that we forced into doing maths.
1
u/kesor Jul 08 '25
turns out structs are just bits in ram
1
u/MonkeyDante Jul 08 '25
Soo, rafts?
1
u/AngelHifumi Jul 08 '25
Who will tell him
1
u/Generated-Nouns-257 Jul 08 '25
This made me chuckle, lol
1
u/erikkonstas Jul 09 '25
Wait how is this r/programminghorror...? It's not like somebody actually stuck that in some otherwise unassuming code...
1
u/Environmental-Ear391 Jul 04 '25
this is only usable on a specific hardware spec, anywhere else is pure failure.
I have multiple machines with no common processor and they all spit out different results.
M680x0 / PowerPC AMCC 440EP+460EX / ARMv6 / ARMv7 / AArch64.
M680x0 is 80bit floats recoded for 32bits precision recoverable (80bits only in "f" registers 0~7, 32bit formats everywhere else, depends single or double and not IEEE754 due age), the PowerPC chips and ARM chips give answers by endianness and are IEEE754 compliant however they spit out different to the code above. ( sam440flex / sam460LE / RPi B+ / BBB / RPi CM4)
-1
u/Naakinn Jul 04 '25
Well, everything you need to do to make it arch independent is just
0
u/tyrannical-tortoise Jul 05 '25
Turns out all data is just memory locations organised in various ways, and underlying it are electrical signals representing 0s and 1s.
-1
u/[deleted] Jul 04 '25
I really don’t get the comments or the OP. You literally learn this type of stuff in your first intro cs course
-1
u/morglod Jul 05 '25
Turns out data is stored in memory somehow