# Is my double a float?

Page 1 of 1

## 10 Replies - 582 Views - Last Post: 05 October 2012 - 08:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294443&amp;s=d31f285dda4369c8970fa30fe03c0d5d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 The3xorc1st

• New D.I.C Head

Reputation: 0
• Posts: 17
• Joined: 05-October 12

# Is my double a float?

Posted 05 October 2012 - 02:33 PM

Hi!
I am new to C programming and have started in class last week. In one of our assignments, as a bonus questions, i have to determine if a double is in fact a float, i have tried everything i can think of but can't seem to find an answer since playing around with floats usually ends with imprecisions, thats what happened when i tried to force a float type on my double and compare it to itself:
```if ((float)var1==var1]{
printf("is a float");
}
else {
printf("is not a float");
}
```
.
the problem with this is that the conversion doesnt seem to work, even if i am working with a really short decimal...

Any help would be greatly appreciated !

Is This A Good Question/Topic? 0

## Replies To: Is my double a float?

### #2 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

## Re: Is my double a float?

Posted 05 October 2012 - 02:52 PM

You would probably be better off comparing the sizeof() the variables. The double usually requires twice the amount of storage.

Jim

This post has been edited by jimblumberg: 05 October 2012 - 02:52 PM

Was This Post Helpful? 0

### #3 The3xorc1st

• New D.I.C Head

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is my double a float?

Posted 05 October 2012 - 02:57 PM

Yes but i declare my variable as a double from the start, even if its content could be fitted in a float, when i use sizeof() itll always return 8 bytes.
Was This Post Helpful? 0

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Is my double a float?

Posted 05 October 2012 - 02:59 PM

A double is a double-pecision floating point number. It's possible for an instance of var1 to equal var2 if one is a float and one is a double (just as 1.0 == 1.000) because the extra digits don't change the value in that case.
Was This Post Helpful? 1

### #5 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

## Re: Is my double a float?

Posted 05 October 2012 - 03:05 PM

Quote

Yes but i declare my variable as a double from the start, even if its content could be fitted in a float, when i use sizeof() itll always return 8 bytes.

You the programmer must make a decision as to which floating point number to use. A float can hold any number a double can hold, but with less precision. I usually recommend using double for most desktop programs, with the exception of some games, because usually people will notice the loss of precision before they notice the differences in speed or storage requirements.

Jim
Was This Post Helpful? 0

### #6 The3xorc1st

• New D.I.C Head

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is my double a float?

Posted 05 October 2012 - 03:07 PM

But don't you agree that 1,51 is a float? if i assign that value to a double though and run my test it returns false...
Was This Post Helpful? 0

### #7 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

## Re: Is my double a float?

Posted 05 October 2012 - 03:16 PM

That's because floating point numbers are not an exact number they are approximations. You should never try to compare floating point numbers using the == operator. Your code is not testing whether the number is a float or a double, it is testing if the two numbers are equal. You may want to read this link about floating point numbers.

Jim
Was This Post Helpful? 0

### #8 The3xorc1st

• New D.I.C Head

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is my double a float?

Posted 05 October 2012 - 03:23 PM

Hmmm I think i get what you mean, would you have an idea then as to how i should proceed?
Was This Post Helpful? 0

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Is my double a float?

Posted 05 October 2012 - 04:07 PM

You could:
• only use float for floating point numbers
• only use double for floating point numbers

Either way though, you need to be careful when comparing two floating point numbers. You might write your own function to compare them:
```bool IsEqual(double numA, double numB) {
// if they're close enough, return true

return false;
}
```

Was This Post Helpful? 0

### #10 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

## Re: Is my double a float?

Posted 05 October 2012 - 04:09 PM

I tried with 1,5 which worked.
Was This Post Helpful? 0

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Is my double a float?

Posted 05 October 2012 - 08:38 PM

It works with 1.5 because that number doesn't repeat in binary. It won't work with any number that does, like 0.2 or 1.0/3.0. It won't work with any number that has more digits than a float can hold.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }