Reputation: 1869
I have a struct like this:
typedef struct tic {
char * close_ts;
float * close;
float * open;
}
open
and close
are float values (like 11.4, 33.5 etc), will be copied from stream but i don't really need to make any computation on them. I just need to extract them and write somewhere else.
Would be better in this case to use char instead of float?
Upvotes: 0
Views: 102
Reputation: 1907
I will disagree with the answers before me. If you do not need to perform any computation on those number, and want to remain as you read them, you should use strings.
Why: Floating point values are imprecise. If you read 1.0f and store it as a float, it might get stored as 0.99999998 (not those exact values mind you). It gets more imprecise as the magnitude of the number increases. If you want to store big numbers, it will only get worse. There is a good article on wikipedia about why this is the case - http://en.wikipedia.org/wiki/Floating_point
If those values are something like money (they look like that from the naming), even if you need to do computations on them, I would advise against float, and would recommend you use some library that can do fixed point math.
Upvotes: 2
Reputation: 8861
First of all, char *close_ts
, float *close
, and float *open
are pointers. You should not assign anything to them but the address of a like variable type. For example:
float num = 5;
float *pNum = #
In this case, pNum
holds the address of num
(&num
). If you dereference pNum
(*pNum
), you get the value of num
(5
).
You should decide which variable type you use depending on what it'll be used for. Will you be storing a character in it, such as 'A'
? If so, use a char
. Will you be storing a number with a fractional part, such as 3.14
? If so, use a float
.
Upvotes: 1
Reputation: 7257
If you want to store float as float it cannot be:
float *open;
//or
float *close;
but
float open;
//or
float close;
If you do not need to perform any operations on these floats - you may also store them as literal character string in an array but its length will depend on your float literal length etc so array may sometimes occupy more space than regular float so float should be ok here.
Upvotes: 1
Reputation: 12885
I see no reason why to use char*
.
The reasons to use float:
sizeof(float) = 4
bytes is less than sizeof("1.234567f") = 9
bytes.Upvotes: 1