5 ms·
Maybe not. The problem is that C++ (before C++20) has no normal print and format function. You supposed to do everything with streams. To switch to two decimal
by colonwqbang 3y ago
Maybe not. The problem is that C++ (before C++20) has no normal print and format function. You supposed to do everything with streams. To switch to two decimal places you would first output some magic value that sets an internal flag in the stream. Then you need to remember to restore it again.
Of course you could just use good old C printf to get some work done. But if you did that the "real" C++ programmers would sneer at you.
- snovv_crash 3y agoprintf is fast, but has safety problems. Also frankly I don't like the fact that the formatting and the variable the data comes from are separated and you have to count out the argument orders to figure which one is which. Python's fstring gets this right.
- another2another 3y agoedit: nvm, looks like I just described fmtlib. I really should keep up..
- josefx 3y ago> Of course you could just use good old C printf The fun thing with printf is that variadic functions cannot handle C++ classes. In the past was doubly fun because compilers proactively compiled an error into the binary instead of raising an error at compile time. So any forgotten c_str() became a crash waiting to happen. C Printf never was, never could be and never will be a suitable way to output data from C++. Now excuse me while I go through the list of thousands of predefined format macros to find out which I need to use to output a uint_fast16_t without making the compiler vomit nonsense.
- deleted 3y ago[deleted]
- jstimpfle 3y ago> C Printf never was, never could be and never will be a suitable way to output data from C++. Now excuse me while I go through the list of thousands of predefined format macros to find out which I need to use to output a uint_fast16_t without making the compiler vomit nonsense. printf("%d\n", (int) myfast16_t); Not that terrible for a type that I've never used, nor seen used.
- josefx 3y agouint_fast16_t is the fastest type (for some undefined meanings of fast) that holds at least 16 bits, I don't think the standard guarantees that it even has to fit into an int, so we are already silently discarding data. Worse if it uses an unsigned int then you are casting from unsigned to signed with a potential undefined singed overflow involved.
- jstimpfle 3y agoI would have assumed that you don't want to use uint_fast16_t to store anything that doesn't fit in 16 bits. If you cast to signed that can't represent the complete range that still wouldn't be called "overflow" AFAIK, and no matter what you call it it is not "undefined". And assuming 32-bit ints there is no loss of information given a 16-bit ints. You can also just cast to unsigned or whatever type you think is enough (you should know). The point is, use a conversion, cast to a simple type, make your code compatible.
- josefx 3y ago> I would have assumed that you don't want to use uint_fast16_t to store anything that doesn't fit in 16 bits. I could have a 16 bit wide bitmask in it, lets flip them ~myMaskFast16, that leaves the higher order bits set to 0xFF... whether I care about them or not. > The point is, use a conversion, cast to a simple type, make your code compatible. Casting to a type that depending on platform may or may not hold enough space to represent the value is not "making it compatible" it makes it non portable.
- IAmLiterallyAB 3y ago
- JohnFen 3y ago> Of course you could just use good old C printf to get some work done. But if you did that the "real" C++ programmers would sneer at you. As someone who has been programming in C++ since before there were C++ compilers (back in the day, you had to run it through a translator to make it into C code, then use a C compiler), I think I'm as real of a C++ programmer as anybody. C++ programmers who sneer at you for this are fully worthy of being ignored. Of course, printf() has its own set of issues as well.