r/shittyprogramming • u/TheCrazyPhoenix416 • Dec 16 '18
Pass nothing my reference?
So I'm trying to use the modf function from cmath, but I don't want to know the integer part, only the fractional part.
Is it possible to put nothing as the reference, so I only get the output from the function?
C++ wants me to use it like thus:
double f3;
double f2 = std::modf(123.45,&f3);I want to be able to do something like (but it doesn't work):
double f2 = std::modf(123.45,nullptr):
32
Upvotes
57
u/green_meklar Dec 16 '18
For the record, this isn't a serious programming sub.
Assuming you're asking a serious question: Why not just make another function that allocates a local variable, calls modf passing that variable's address, and then returns just the integer part? If you're lucky, the compiler may even optimize away the useless code and leave you with no performance drop.
3
u/TheCrazyPhoenix416 Dec 16 '18
EDIT: Should be "Pass nothing by reference" as title.
48
u/PM_ME_YOUR_HIGHFIVE Dec 16 '18
use JQuery
7
u/hephaestos_le_bancal Dec 16 '18 edited Dec 16 '18
No you have to provide a valid reference. Probably for a valid reason, because the underlying algorithm uses it directly for its calculation, so you get to control this resource.
The alternative you have in that particular case is to use another rounding function. As far as I understand it, std::truncf should do the trick.
5
u/ShitTalkingAssWipe Dec 16 '18
Not a meme answer here:
Is it possible to do
2
u/G01denW01f11 Dec 16 '18
This compiles fine and returns the correct result with
-Wall -Wextra. (Please don't actually do anything like this ever.)Being serious and pedantic for a sec, this is passing a pointer by value, not passing by reference.
1
u/SupermanLeRetour Dec 16 '18
You have to provide a valid pointer to double because the underlying algorithm uses it to compute the integral part. According to cppreference, here's how it is implemented :
}
If you pass a nullptr, the following lines will not be valid :
You can re-create the function and make it use an local double to store the integral part. Either modify this one, or wrap the standard function inside your own.
By the way, there's no reference involved here. modf asks for a pointer to double, not for a reference (that is, an address which point to a place where you can store a double).
1
u/[deleted] Dec 16 '18
Easy, just hand it a valid pointer to part of the stack downstack from the current stack frame and let it trash some unused memory. This is pretty simple with inline assembly.