float multiplication problems

This is a discussion on float multiplication problems within the C Programming forums, part of the General Programming Boards category; The exercise: Write a function called strToFloat that converts a character string into a floating point value. Have the function ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    158

    float multiplication problems

    The exercise:

    Write a function called strToFloat that converts a character string into a floating point value. Have the function accept an optional leading minus sign. So, the call

    strToFloat("-867.6921")

    should return the value -867.6921.
    The test value is 123.123 in character format.

    All is going well except something with my last subroutine with switch case statement multiplicationa is off. Got me scratching my head. Here's the output.

    Code:
    The number of decimal places are: 3
    Decimal character string extracted to buffer: 123123
    Decimal result=123123
    Converted completely back to decimal: 123.123001
    Here is the switch case structure. I'm not sure if its my initializing of floats I'm doing wrong or what.

    Code:
    void decPlaceMult(double number)
    {
    	float answer=0;
    	
    	switch(count)
    	{
    	case 1:
    		answer=number*.1;
    		break;
    
    	case 2:
    		answer=number*.01;
    		break;
    
    	case 3:
    		answer=number*.001;
    		break;
    
    	case 4:
    		answer=number*.0001;
    		break;
    
    	case 5:
    		answer=number*.00001;
    		break;
    		
    	default:
    		break;
    	}
    
    	printf("Converted completely back to decimal: %f",answer);
    	getchar();
    
    	return;
    }
    Any insight greatly appreciated.
    Last edited by A34Chris; 02-12-2012 at 07:20 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I don't see where "count" is defined...

    But as for your problem...that function would be called atof().

    Code:
    float strToFloat(char *input)
    {
        return (float)(atof(input)); 
    }


  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    158
    Quote Originally Posted by memcpy View Post
    I don't see where "count" is defined...

    But as for your problem...that function would be called atof().

    Code:
    float strToFloat(char *input)
    {
        return (float)(atof(input)); 
    }

    Yeah that's cheating.

    And count is global.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    I've got one word for you: Rounding Errors

    EDIT: Ok, maybe two words
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    158
    Quote Originally Posted by GReaper View Post
    I've got one word for you: Rounding Errors

    EDIT: Ok, maybe two words
    How can I get it to round? I browsing the book here but cant see a solution to it in the float references.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by A34Chris View Post
    How can I get it to round?
    You can't, at least not the way you think. You can just ask from printf to only print eg 3 fractional digits with "%.3f"
    Devoted my life to programming...

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    As long as the input isn't too crazy I would actually store the place values as a (big) integer and then put the decimal point where it needs to be by multiplying by a power of ten, and maybe -1.0 for the sign. Making sure when I multiply that the result is a floating point of course.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    158
    ok thanks for your guys input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Float: Should use Division or Multiplication
    By hqt in forum C Programming
    Replies: 8
    Last Post: 01-01-2012, 02:30 AM
  2. Float problems
    By Cyantwo in forum C Programming
    Replies: 2
    Last Post: 02-27-2011, 02:41 PM
  3. float multiplication and subtraction
    By panos in forum C Programming
    Replies: 4
    Last Post: 07-16-2008, 01:29 AM
  4. Beginner's C, problems with double, float
    By wileyokiley in forum C Programming
    Replies: 4
    Last Post: 08-17-2007, 08:06 AM
  5. Problems with addition of float variables
    By Phanixis in forum C Programming
    Replies: 5
    Last Post: 07-24-2003, 06:22 PM

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