'float' to 'double' conversion warning

This is a discussion on 'float' to 'double' conversion warning within the C Programming forums, part of the General Programming Boards category; Pls help me solve this guys.... the code is as below: Code: #include <stdio.h> #include <math.h> //#define A 90 //#define ...

  1. #1
    Achit
    Guest

    Unhappy 'float' to 'double' conversion warning

    Pls help me solve this guys....
    the code is as below:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    //#define A 90
    //#define B 180
    //#define C 360
    #define PI 3.1416
    
    void main(void)
    {
                   char choice;
                   float voltage, current, totalPower, angle;
    
                   printf("MENU");
                   printf("\n=====");
                   printf("\nFinding the total power for different angles:");
                   printf("\nA. 90");
                   printf("\nB. 180");
                   printf("\nC. 360");
                   printf("\nD. Others");
                   printf("\nE. Quit");
                   printf("\n\nYour choice: ");
                   scanf("%c", &choice);
    
                  
                   switch(choice)
                              {
                                      case 'A' :angle = 90;
    		           printf("Enter the voltage(V):");
    		            scanf("%f", &voltage);
    			printf("Enter the current(A):");
    			scanf("%f", &current);
    			totalPower =  voltage * current * cos((angle * PI) / 180);
    			printf("Total power = %.2fw\n", totalPower);
    									break;
    
                                      case 'B' :angle = 180;
    									printf("Enter the voltage(V):");
    			scanf("%f", &voltage);
    			printf("Enter the current(A):");
    			scanf("%f", &current);
    			totalPower =  voltage * current * cos ((angle * PI) / 180);
    			printf("Total power = %.2fw\n", totalPower); 
    									break;
     
                                 
                                      case 'C' :angle = 360;
    			printf("Enter the voltage(V):");
                                                scanf("%f", &voltage);
                                                printf("Enter the current(A):");
                                                scanf("%f", &current);
                                                totalPower =  voltage * current * cos ((angle * PI) / 180);
                                                printf("Total power = %.2fw\n", totalPower); 
       
                                      case 'D' :printf("Enter the angle:");
    			scanf("%f", &angle);
    			printf("Enter the voltage(V):");
    			scanf("%f", &voltage);
    			printf("Enter the current:");
    			scanf("%f", &current);
    			totalPower = voltage * current * cos ((angle * PI) / 180);
    			printf("Total power = %.2fw\n", totalPower);
    									break;
    
                                     case 'E' :printf("Bye-Bye!\n");
    			break;
    
                                     default :printf("Invalid Option!!\n");           //none of the above
                                              break;
                              } //end switch
                               
    } //end main
    Code tags added by Kermi3 - Disabled smiles too

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    First of all, before anyone help you, edit your post after reading this .

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    A float to double warning means that your program needs a cast somewhere. It's that simple. Or, if you want, you could ignore it. A float to double conversion can't really cause any harm.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    #1 - Use code tags
    #2 - Turn off smilies when you post code. The checkbox is right below where you type.
    #3 - cos(x) returns a value of type double. When you are multiplying a float*double, the float is increased to the precision of a double for the operation. However, that's silly when you are storing the result in a float. So use this instead:

    Code:
    (float)cos((angle * PI) / 180);
    Oh, and one more, completely off topic thing...200th post
    Away.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Or you can use the appropriate math functions that return floating point precision ( cosf )

    Code:
    totalPower = voltage * current * cosf ((angle * PI) / 180.0f);
    Notice when you have 180.0 that is really a double. You need to put the f afterwards to say you want it to be a float. You actually had 180 which would be an integer value.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Achit
    Guest

    Cool

    For all the replies....thank u great people...
    The solutions really meant a lot to me...
    Ehmm...sorry for posting the message unpresentably...
    It was my first message posting here...(' ,')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 07:47 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21