casting the type not only the representation

G

Gary Wessle

Hi there

I have a method which returns time_t and another two methods return
double data types and I cann't change that since the library is
provided by Big Bucks Inc. I think time_t is long but I could not
verify that from time.h

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.

in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
....
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don't use "\n" thus extra work when extracting the data but that is
ok.

the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float> (the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?

many thanks.
 
A

Alf P. Steinbach

* Gary Wessle:
Hi there

I have a method which returns time_t and another two methods return
double data types and I cann't change that since the library is
provided by Big Bucks Inc. I think time_t is long but I could not
verify that from time.h

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.

in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
...
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don't use "\n" thus extra work when extracting the data but that is
ok.

the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float> (the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?

First, you're confusing several things. You're confusing binary
representation with textual representation. And you're confusing the
properties of a C++ implementation with what the standard guarantees.

Second, you're optimizing prematurely, which is Evil(TM).

Think about it: there are 1024 MiBs in one GiB. Each week, unoptimized,
you produce a file about 1.5 Mib. Well, even with just a 1 GiB disk
that can go on for at least ten years. Current disk sizes are typically
not less than 100 GiB. Which means at least a thousand years (the disk
won't last that long, but has the capacity): /why are you optimizing/?
 
I

Ian Collins

Gary said:
Hi there

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.
Not a portable assumption.
in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
....
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don't use "\n" thus extra work when extracting the data but that is
ok.
Why not just save the data as is? If the size bothers you, just compress
the file.
the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float> (the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?
Do you want a text file or a binary file?
 
G

Gary Wessle

if I do
float a;
float b;
a = static_cast<float> (output of the double method here);
b = static_cast<float> (output of the double method here);
cout << a << " " << b << " ";

I get
dat_col.cpp:171: warning: name lookup of ‘b’ changed
dat_col.cpp:137: warning: matches this ‘b’ under ISO standard rules
dat_col.cpp:141: warning: matches this ‘b’ under old rules

well, is there not a warning with a, then why it did not catch it and
it only cough b, and do I have to take this warning seriously?

thanks
 
I

Ian Collins

Gary said:
if I do
float a;
float b;
a = static_cast<float> (output of the double method here);
b = static_cast<float> (output of the double method here);
cout << a << " " << b << " ";
Why bother casting to float?
I get
dat_col.cpp:171: warning: name lookup of ‘b’ changed
dat_col.cpp:137: warning: matches this ‘b’ under ISO standard rules
dat_col.cpp:141: warning: matches this ‘b’ under old rules
How can we tell without an example?
 
J

Jerry Coffin

[ ... ]
the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float> (the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

I think you've misinterpreted the situation. operator<< (at least
normally) does _formatted_ output. That means your value (double or
float) is converted to text before being written. To write the bytes of
the float itself, you usually want to use ostream::write:

float x = static_cast<float>(double_value);

your_file.write((char *)&x, sizeof(x));

Given the constant number of records, it sounds a lot like you have a
fixed time increment between the values -- if, for whatever reason, you
really need to save space, you can probably do quite nicely writing a
single time stamp at the beginning of the file, then read it and compute
the time stamp for any other record.
 
J

John Harrison

Gary said:
if I do
float a;
float b;
a = static_cast<float> (output of the double method here);
b = static_cast<float> (output of the double method here);
cout << a << " " << b << " ";

I get
dat_col.cpp:171: warning: name lookup of ‘b’ changed
dat_col.cpp:137: warning: matches this ‘b’ under ISO standard rules
dat_col.cpp:141: warning: matches this ‘b’ under old rules

well, is there not a warning with a, then why it did not catch it and
it only cough b, and do I have to take this warning seriously?

thanks

A complete example is needed. The explanation lies somewhere in the code
you missed out.

John
 
G

Gary Wessle

Ian Collins said:
Not a portable assumption.

Why not just save the data as is? If the size bothers you, just compress
the file.
there are 33 files and all open to get data from to run a code.
I can compress them when not in use.
 
G

Gary Wessle

John Harrison said:
A complete example is needed. The explanation lies somewhere in the
code you missed out.

John

float is of size 4 and double is of size 8 on my machine.
the float type is big enough and no need for double.
example of code:

class A
{
string nam;
double a, b;
time_t c;
public:
/* ... */
A(string nam): nam(nam) {};
double get_a (){return a;}
double get_b (){return b;}
time_t get_c (){return c;}
};


/ map<string,A> m_a has been populated */

for( vector<string>::iterator i = _unique.begin(); i != _unique.end(); i++ )
{
{
timstmp = static_cast<int> (m_a[*i].get_c());
a = static_cast<float> (m_a[*i].get_a());
b = static_cast<float> (m_a[*i].bet_b());
cout << timstmp << " " << a << " " << b << endl;
}
}
 
I

Ian Collins

Gary said:
float is of size 4 and double is of size 8 on my machine.
the float type is big enough and no need for double.
example of code:
<snip iffy code>

The code you posted can't be from your application, there were way to
many errors. Post something that causes the warnings you quoted.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

there are 33 files and all open to get data from to run a code.
I can compress them when not in use.



text.

Then the question is not whether you use a float or a double but
rather how much precision you want. On my machine an int is 4 bytes
but the largest number I can store is 2147483647, which will take 10
bytes stored as text. The same goes for the doubles/floats, by setting
some flags of the ostream you can control how many decimals of the
number you want to store but remember, if the number is negative just
the minus sign will take one byte.

And as others have pointed out, this is really not the place to
optimize, if space is a problem then perhaps you should consider
starting a new file every month or so and compressing the old ones.
Files with this kind of contents (13-14 differench characters) will be
very compressible.
 
G

Gary Wessle

Erik Wikström said:
Then the question is not whether you use a float or a double but
rather how much precision you want. On my machine an int is 4 bytes
but the largest number I can store is 2147483647, which will take 10
bytes stored as text.

how is that? can you explain please.
 
G

Guest

how is that? can you explain please.

Since you store it as plain text each character will take one byte and
there are 10 digits in the above number. How many digits a float/double
will have depends on how much precision you want when you print the
number, then you get an extra byte for the decimal-point and perhaps one
for a minus-sign.
 
G

Gary Wessle

Erik Wikström said:
Since you store it as plain text each character will take one byte and
there are 10 digits in the above number. How many digits a
float/double will have depends on how much precision you want when you
print the number, then you get an extra byte for the decimal-point and
perhaps one for a minus-sign.

so using float instead of double will save memory during running but
not with file storage, "did you say, unless we store it as binary?"
which I am not ready to get into for now, but just wanted to have some
general idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top