-
March 17th, 2018, 01:22 AM
#1
[RESOLVED] Writing an extremely tiny number to disk
Code:
float matWorld[16]
errno_t err = _wfopen_s( &fFile, File.c_str(), L"wb" );
fwrite( (void*) matWorld, sizeof(float), 16, fFile);
The numbers are as small as these
Code:
1.56768e-007 -2.36557e-009 1.96187e-011 0
2.36557e-009 1.56768e-007 -4.46082e-012 0
-1.95498e-011 4.76264e-012 1.56786e-007 0
-0.000124691 3.03403e-005 -0.000232232 1
The results of the save are nearly all zeros....
Code:
results:
0.000000,-0.000000,0.000000,0.000000,0.000000,0.000000,-0.000000,0.000000,-0.000000,0.000000,0.000000,0.000000,-0.000125,0.000030,-0.000232,1.000000;;
Is there a way to save the small numbers with scientific notations?
thanks
Jack
Last edited by luckiejacky; March 17th, 2018 at 01:26 AM.
-
March 17th, 2018, 03:12 AM
#2
Re: Writing an extremely tiny number to disk
Originally Posted by luckiejacky
...
The results of the save are nearly all zeros....
Code:
results:
0.000000,-0.000000,0.000000,0.000000,0.000000,0.000000,-0.000000,0.000000,-0.000000,0.000000,0.000000,0.000000,-0.000125,0.000030,-0.000232,1.000000;;
But how do you open/read this file to see the results?
Victor Nijegorodov
-
March 17th, 2018, 08:02 AM
#3
Re: Writing an extremely tiny number to disk
Consider
Code:
#include <cstdio>
#include <string>
using namespace std;
int main()
{
using numtype = float;
const string File= "num.bin";
const numtype matWorldOut[] = { 1.56768e-007, -2.36557e-009, 1.96187e-011, 0,
2.36557e-009, 1.56768e-007, -4.46082e-012, 0,
-1.95498e-011, 4.76264e-012, 1.56786e-007, 0,
-0.000124691, 3.03403e-005, -0.000232232, 1};
const size_t noElems = sizeof(matWorldOut) / sizeof(matWorldOut[0]);
numtype matWorldIn[noElems];
FILE* fFile;
if (fopen_s(&fFile, File.c_str(), "wb")) {
cout << "Error opening file for write" << endl;
return 1;
}
if (fwrite((void*)matWorldOut, sizeof(numtype), noElems, fFile) != noElems) {
cout << "Error writing data" << endl;
return 2;
}
fclose(fFile);
if (fopen_s(&fFile, File.c_str(), "rb")) {
cout << "Error opening file for read" << endl;
return 1;
}
if (fread((void*)matWorldIn, sizeof(numtype), noElems, fFile) != noElems) {
cout << "Error reading data" << endl;
return 2;
}
cout << "Written data" << endl;
for (const auto& n : matWorldOut)
cout << n << " ";
cout << endl << endl << "Read data" << endl;
for (const auto& n : matWorldIn)
cout << n << " ";
cout << endl;
return 0;
}
which displays
Code:
Written data
1.56768e-07 -2.36557e-09 1.96187e-11 0 2.36557e-09 1.56768e-07 -4.46082e-12 0 -1.95498e-11 4.76264e-12 1.56786e-07 0 -0.000124691 3.03403e-05 -0.0
00232232 1
Read data
1.56768e-07 -2.36557e-09 1.96187e-11 0 2.36557e-09 1.56768e-07 -4.46082e-12 0 -1.95498e-11 4.76264e-12 1.56786e-07 0 -0.000124691 3.03403e-05 -0.0
00232232 1
where the read data is the same as the written data.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 18th, 2018, 01:52 AM
#4
Re: Writing an extremely tiny number to disk
Originally Posted by luckiejacky
Is there a way to save the small numbers with scientific notations?
If you store a float in binary format on file and read it back again, it will be the same. All 32 bits of the float stay intact. The same float may appear different on screen but this is determined by which format you use to display it. "Scientific notation" is one possibility, "Fixed" is another.
You don't have to worry about a float being small (or big). It will have the same precision (6-7 accurate digits) throughout the whole of its valid range (from about +-10^-34 up to +-10^34). (If you accidentally use a float outside these limits it will enter a state of underflow/overflow which you can check for).
Last edited by wolle; March 19th, 2018 at 01:54 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|