tom
tom

Reputation: 14513

Objective C - How to convert double to long

I have a double value 1318611498349.3559, I wanted to convert it to long by doing the following, but I am getting a value of -4506682935000170496, which seems wrong to me.

double dv = ...;
long lv = (double)dv;
...

Where can I find some documentation on this?

Upvotes: 2

Views: 10854

Answers (6)

vpathak
vpathak

Reputation: 1163

I use this for storing the seconds since epoch. This requires conversion because the timeIntervalSince1970 returns a double. In my case overflow can not happen (at least in near future), but you may add a test of exceeding LONG_MAX...

NSDate * now = [NSDate date] ; NSNumber * seconds_since_epoch = [[NSNumber numberWithDouble:[now timeIntervalSince1970]] longValue];

Upvotes: 0

Russ
Russ

Reputation: 1844

The other answers are correct in that a double can hold values much larger than any integral type, however if the double value is within range it would be nice to be able to convert.

The reason the compiler isn't barfing at you for trying to just cast a double into a long is because you can actually do it. Obviously everything in a computer is stored as strings of 1's and 0's, basically what your code is doing is saying take vIn and interpret the bits as a long integer value. Taking the bits of a floating point value and interpreting them as an integral type is rarely useful and the value is no where near what the double represents.

I was actually trying to do the opposite. I needed to convert a long (actually long long) to a double. Then it hit me, let NSNumber do the conversion. Create a temporary NSNumber object with the value you want, then use the method to get the type of value you want back.

double vIn = 123.4321;
long vOut = [[NSNumber numberWithDouble:vIn] longValue];

No guarantees this will do what you want. I'm guessing if the double value is larger than what a long can store it will just return the max value.

Upvotes: 5

Lou Franco
Lou Franco

Reputation: 89172

A double can represent numbers of much higher magnitude than a long because it is represented by a number of significant digits and an exponent to set its magnitude.

The result is that the range of values is much larger than long can handle.

More information here:

http://en.wikipedia.org/wiki/Floating_point

It would not matter if you used a floating point type with the same number of bits as an integral type -- that's not the real cause of the problem -- it's the fact that floating-point was designed to represent a much larger range.

For example, the maximum 4-byte floating-point number cannot be represented in an 8-byte integral type.

Upvotes: 0

Nate the Noob
Nate the Noob

Reputation: 370

double vIn = 0.0;
long vOut = (long)vIn;

Should be the way to convert.

Upvotes: 0

bbum
bbum

Reputation: 162712

LONG_MAX is 0x7FFFFFFFL or 2147483647.

I.e.:

2147483647

Is a whole lot less than:

1318611498349.3559

I'm surprised the compiler isn't barfing up a conversion error/warning of some kind because, no matter what you do, 64 pounds of bits just ain't gone fit in a 32 pound sack.

Upvotes: 0

sticksu
sticksu

Reputation: 3738

It's wrong because a double it's represented on 8 bytes and long is on 4 bytes, so the double number is too big.

Upvotes: 2

Related Questions