I have a problem with casting from float to double when fread;
fread(doublePointer,sizeofFloat,500,f);
if i change double pointer to float pointer, it works just fine.
However,i need it to be double pointer for laster on, and i thought when i write from small data type (float)to bigger data type(double)’s memory, it should be fine. but it turns out it doesnt work as i expected.
what is wrong with it, and how do i solve this problem.
i know i can solve it by converting it one by one. but i have a huge amount of data. and i dont wanna extra 9000000+ round of converting.. that would be very expensive. and is there any trick i can solve it?
is there any c++/c tricks
thanks
If you write float-formatted data into a double, you’re only going to get garbage as a result. Sure, you won’t overflow your buffer, but that’s not the only problem – it’s still going to be finding two floats where it expects a double. You need to read it as a float, then convert – casting (even implicitly) in this manner lets the compiler know that the data was originally a float and needs to be converted: