10 Replies - 388 Views - Last Post: 05 October 2012 - 08:38 PM Rate Topic: -----

#1 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ! :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Is my double a float?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • 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  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,507
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • 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