ÐΞΛÐ HΛϾКΞЯ
Tute
Hi Do you have any idea how to show the binary form of a double?
You can use an union: // in C you have to put typedef, in C++ is not necesary typedef union { double d; uint8_t bytes[sizeof(double)]; } double_bytes_t; // ... double_bytes_t x; x.d = 2.3; // supposing little endian: for(int i = sizeof(double)-1; i >= 0; i--) { printf("%02X", x.bytes[i]); // HEX format of binary for IEEE754 64-bits }
Suraj
Anyone can send a code add a two matrix using c .
Maxim
I think that the correct way is to use std::bit_set. I can not give you an example right now, but you can google it
Tute
You probably can, but it is an undefined behavior, you can read about it here: https://en.cppreference.com/w/cpp/language/union
Aha, i see, but in C with most of the compilers i assure you that this works just fine... The only thing is knowing if the hardware system is little endian or big endian... but most are little endian.
Maxim
You meant to say std::bit_cast.
No, I meant to say std::bitset, not std::bit_set :) But yes, probably std::bit_cast will be needed
Peppe
what is the probelm?
Tute
No, I meant to say std::bitset, not std::bit_set :) But yes, probably std::bit_cast will be needed
in C++ i will go for std::bitset, i know, but assuming this is a student's cuestion, not a professional, i asume the teacher is looking for a more raw algorithm method...
Tute
You must ensure C code will be compiled. Elsewise the compiler you choose will make asumptions for you
This union example i showed will work always in every C compiler... try it yourself hehe
Jose
This union example i showed will work always in every C compiler... try it yourself hehe
Gcc with -std=c++11, for example, will not work as expected. And it's a C compiler (among other languages, too) Yes, I'm going to extreme cases, but this is an example
Anonymous
C++ support std::optional Instead of union
std::optional is not a replacement for union. std::variant is.
Anonymous
std::optional is not a replacement for union. std::variant is.
I thought std:: optional is for handling not critical exceptions to "substitute" try catch
Dm
I thought std:: optional is for handling not critical exceptions to "substitute" try catch
Or maybe suitable for case of returning pointer which can be nullptr?
Anonymous
I thought std:: optional is for handling not critical exceptions to "substitute" try catch
No that is std::expected. std::optional is used to represent a value if it exists or that it does not exist (equivalent of null) if there is no meaningful value yet. It can be used for error handling in a limited sense but it is not a replacement for try-catch
Anonymous
Or maybe suitable for case of returning pointer which can be nullptr?
Yeah, when we don't get null pointer or things like this
Anonymous
I've recently stumbled upon the article with a somewhat similiar idea, and actually I wanted to share godbolt code, taken from the same article, where some form of vectorization is achieved: shorturl.at/xFGT7 https://siboehm.com/articles/22/Fast-MMM-on-CPU You mentioned spatial locality: can I increase it with a contiguous pool of memory in order to make the algorithm more cache friendly in this regard? I can't use stack for the pool because there will be enormous amounts of data.
No. You can't effectively increase spatial locality in matrix multiplication. The way to increase spatial locality is not to have the 2nd matrix in row major form. It must be converted to a column major form and then the matrix multiplication needs to be done in an inverted sense. This will be easily vectorized by the compiler. The reason why spatial locality is not there in the first case is because you are picking elements off a column in the 2nd matrix in regular matrix multiplication and these elements are far away in memory and not more than 1 or 2 elements can fit in the same cache line depending on the size of the row in the 2nd matrix and also on the cache line size.Replacing the 2nd matrix by its transpose addresses this issue(computing transpose is a n^2 operation by itself and order n^2 space complexity). Now you will be able to compute dot products across rows of both the matrices which is a cache friendly operation. And this can be auto vectorized by the compiler. If you want to avoid computing the transpose (which you would if your matrix is huge and you want to avoid the space overheads), you must vectorize the code yourself as the compiler won't be of much help like I said earlier.
Alireza
I made a linked list and now I want to pop() all of its nodes but something is keeping my mind busy!!! When my linked list has got one node I must delete (Head) which is an attribute in my class , but it does not delete like others normal pointers !!! why??
Who can help me with <cmath> ?
DaviChan
What is the difference between iostream and studio.h?
stdio is pnly kept for combatibility with C. streams are the new way to do i/o in C++. Streams have a slight overhead, why some still prefer sprintf and puts.
DaviChan
And do we use all of them?
There are many and yes, all og them have a valid use case
Pavel
Who can help me with <cmath> ?
This is sooo vague question, can you get directly to the point? dontasktoask.com
Well, I don't understand one problem, I would like you to explain to me how to solve it, but I didn't ask you all to solve the problem completely
Marco
Hello! I have a question about unique_ptr. I want to create an array of pointers of a superclass, so then with the magic of polymorphism, I insert objects of type of the subclass (obviously dynamically allocated). What I wanted to ask is: do these two lines of code ALL handle the new and delete issue by THEM? (so I don't have a problem, I know that if an element is removed from the vector, the space in heap memory is deleted by itself). vector<std::unique_ptr<BaseClass>> my_vector; my_vector.emplace_back(std::make_unique<SubClass>(parameters, ...));
klimi
vector calls deallocation that would call delete on unique_ptr, so you should be safe :)
Marco
thanks!
Dm
vector calls deallocation that would call delete on unique_ptr, so you should be safe :)
I got also questions about unique ptr. Is it possible to pass list of unique ptrs to constructor without implementing your own list derived from std::list? I mean if you pass list of unique ptrs to constructor and then assign it to some variable it will failed but maybe I did somethings wrong (I used std::move)
Dm
I am not sure about this one, it seems like it should be possible but i cannot give certain answer (and i don't have time to research it rn)
it failed because of the problems with move, but I don't know how to fix it and is it possible or not.
Anonymous
https://godbolt.org/z/7KaodE1hr
You are trying to move from a const reference to std::list. How would moving work if the to be moved from object is const? This would try calling the copy constructor of the list which would fail because the unique_ptr's copy constructor is deleted
Dm
https://godbolt.org/z/7KaodE1hr
ow I'm stupid thanks
Hema Hariharan
#include <stdio.h> int main() { int i=10,j; j= ++i + ++i; printf("\n%d",j); return 0; }
Hema Hariharan
Can anyone explain this snippet
Hema Hariharan
Logically op=23 I got=24
Hema Hariharan
Why?
ㅤㅤㅤㅤㅤ
Why?
j=0
Dm
++i return reference to i in this case we got this: 12 + 12
Hema Hariharan
Sir output is 23
24 is the crt output i am looking for the reason
Dm
https://godbolt.org/z/fzcdW5EjM
Hema Hariharan
Re run
Same coming
Dm
Sir output is 23
do you use C++ compiler or C?
klimi
24 is the crt output i am looking for the reason
i = 10 ++i i = 11 ++i i = 12 i+i = 12+12 = 24
klimi
11+12?
where?
Dm
https://en.cppreference.com/w/cpp/language/operator_incdec prei-ncrement return reference
klimi
you have j = (++i) + (++i)
Deepak Chaurasia
klimi
That what i said
you said 11+12
Deepak Chaurasia
11+12
klimi
it's 12+12
klimi
i + i
Deepak Chaurasia
Dm
11+12
please share you test code
Hema Hariharan
i = 10 ++i i = 11 ++i i = 12 i+i = 12+12 = 24
If this is the logic then try this
Hema Hariharan
#include <stdio.h> int main() { int i=1,j; j= ++i + ++i + ++i; printf("\n%d",j); return 0; }
Deepak Chaurasia
please share you test code
23 [Program finished]
Dm
23 [Program finished]
C++ code (godbolt/pastebin whatever)
Hema Hariharan
9?
10 coming