Thread: Ok I'm stuck with a double to float error

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Ok I'm stuck with a double to float error

    Here's the snippet of code that I'm having problems with:

    Code:
                   
    
                    int            customernumber;
    	float	waterlevel;
    	float	previouslevel;
    	float	totalwater;
    	float	totalbill;
    	float	amountowed;
    
    
    	printf("Enter customer number\n" );
    
    	scanf( "%d" ,  &customernumber );
    
    	printf("Enter current water level\n" );
    
    	scanf("%f" , &waterlevel );
    
    	printf("Enter previous water level\n" );
    
    	scanf("%f", &previouslevel );
    
    	totalwater = waterlevel - previouslevel;
    
    	totalbill = 30 + (totalwater/1000  * .55);
    
    	amountowed = totalbill;
    I keep getting this warning "conversion from double to float, possible loss of data" for this line

    totalbill = 30 + (totalwater/1000 * .55);

    I dont understand what it could mean, everything is declared as float ( I, obviously, made sure of that), if anyone has any idea what's wrong with this line I would really appreciate it if ya could share it with me.
    Thank you very much.
    Last edited by Extropian; 06-06-2005 at 11:24 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    .55 by itself is a double constant.
    Since the rest of the expression is float, I think it's this one which making the whole of the right hand side into a double expression.

    So when you perform the assignment, the now double expression on the right is being converted to a float.

    .55F is the way to define a float constant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you could just add a cast:

    Code:
    totalbill = (float) (30 + (totalwater/1000 * .55));
    [EDIT]
    Or maybe declare everything as double and not as float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM