473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

casting the type not only the representation

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.


Feb 4 '07 #1
13 1682
* 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/?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 4 '07 #2
Gary Wessle wrote:
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?

--
Ian Collins.
Feb 4 '07 #3
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
Feb 4 '07 #4
Gary Wessle wrote:
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?

--
Ian Collins.
Feb 4 '07 #5
In article <m3************@localhost.localdomain>, ph****@yahoo.com
says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 4 '07 #6
Gary Wessle wrote:
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
Feb 4 '07 #7
Ian Collins <ia******@hotmail.comwrites:
Gary Wessle wrote:
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.
there are 33 files and all open to get data from to run a code.
I can compress them when not in use.
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?
text.
--
Ian Collins.
Feb 5 '07 #8
John Harrison <jo*************@hotmail.comwrites:
Gary Wessle wrote:
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
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,Am_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;
}
}
Feb 5 '07 #9
Gary Wessle wrote:
John Harrison <jo*************@hotmail.comwrites:

>>Gary Wessle wrote:
>>>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


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.

--
Ian Collins.
Feb 5 '07 #10
On Feb 5, 2:00 am, Gary Wessle <phd...@yahoo.comwrote:
Ian Collins <ian-n...@hotmail.comwrites:
Gary Wessle wrote:
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.

there are 33 files and all open to get data from to run a code.
I can compress them when not in use.
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?

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.

--
Erik Wikström

Feb 5 '07 #11
"Erik Wikström" <er****@student.chalmers.sewrites:
On Feb 5, 2:00 am, Gary Wessle <phd...@yahoo.comwrote:
Ian Collins <ian-n...@hotmail.comwrites:
Gary Wessle wrote:
Hi there
using sizeof(type) and numeric_limits<type>::max() tells me that int
...
how do I solve this?
Do you want a text file or a binary file?
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.
how is that? can you explain please.
Feb 5 '07 #12
On 2007-02-05 16:56, Gary Wessle wrote:
"Erik Wikström" <er****@student.chalmers.sewrites:
>On Feb 5, 2:00 am, Gary Wessle <phd...@yahoo.comwrote:
Ian Collins <ian-n...@hotmail.comwrites:
Gary Wessle wrote:
Hi there

using sizeof(type) and numeric_limits<type>::max() tells me that int
...
how do I solve this?

Do you want a text file or a binary file?

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.

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.

--
Erik Wikström
Feb 5 '07 #13
Erik Wikström <Er***********@telia.comwrites:
On 2007-02-05 16:56, Gary Wessle wrote:
"Erik Wikström" <er****@student.chalmers.sewrites:
On Feb 5, 2:00 am, Gary Wessle <phd...@yahoo.comwrote:
Ian Collins <ian-n...@hotmail.comwrites:
Gary Wessle wrote:
Hi there

using sizeof(type) and numeric_limits<type>::max() tells me that int
...
how do I solve this?

Do you want a text file or a binary file?

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.
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.

--
Erik Wikström
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.
Feb 5 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
32
by: 127.0.0.1 | last post by:
Hello! #include <stdio.h> int main(void) { char *p = "hello, world"; printf("%p\n", p);
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
19
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long*...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
12
by: candy_init | last post by:
I recently came across the following CAST macro which can cast anything to any other thing: #define CAST(new_type,old_object) (*((new_type *)&old_object)) union { char ch; int i; }...
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
21
by: Angus | last post by:
I need to convert a double to a 64 bit integer. I am using __int64 but could of course use a more portable type. To perform the correct casts: double d = 3.3; __int64 i64; i64 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.