Jump to content

Floating point

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok, so I'm just getting the hang of this floating point thing and I decided to write a couple of quick programs to illustrate the difference between that and int. I got it to divide 5/2 and get 2 with the variable being an integer but this code isn't working to make it equal to 2.5:


#include<stdio.h>

float main()
{
float x;

x=5/2;

printf("%f", x);
}

Any help please?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
5/2 is ALWAYS 2, regardless of the data type being assigned to.
5/2.0 is 2.5.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
Oh OK, thanks very much

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Also, it's int main and not float. You can read more about that here and here.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users