Declare float

This is a discussion on Declare float within the C Programming forums, part of the General Programming Boards category; Hello, I declared a float array in my program as follows: Code: float example_array[] = {0.2, 0.1}; when I look ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    8

    Declare float

    Hello,

    I declared a float array in my program as follows:

    Code:
    float example_array[] = {0.2, 0.1};
    when I look at the debugger while running my program; it says that the array holds 0.20000003....

    Due to this my program does not work. Can anyone help me here?

    Thanks

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Floats are not exact. There are certain numbers for which no perfect binary equivalent exists. But as you can see the FPU is fairly precise unless you are trying to go the the moon or something.

    Also try adding f to the end of your floats.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ever looked at the moon and just thought about stuff. Like space stuff.

    It makes me sad for some reason.

    EDIT: I just googled "we live in a piece of software" and there was practically nothing. I mean it's a prefectly possible theory. Because I'm weird and have a large amount of free time (well now anyways) I've been thinking about how practically nothing in what we know to be the Universe actually proves that we aren't living in a piece of software.

    The real question is who wrote it? The old cliché of a super- intelligent alien race is a bit "meh", I would say it's a more technologically advanced us wanting to simulate the world as it was. Why? How the hell should I know it's just a theory and it's more interesting than aliens.
    Last edited by cboard_member; 04-09-2006 at 08:15 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

    Code:
    #include <math.h>
    
    int close(float a, float b)
    {
        if(fabsf(a - b) < .01)
            return 1;
        else
            return 0;
    }

  5. #5
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Quote Originally Posted by joed
    What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

    Code:
    #include <math.h>
     
    int close(float a, float b)
    {
    if(fabsf(a - b) < .01)
    return 1;
    else
    return 0;
    }
    Note that <limits.h> defines constants you may find useful for this purpose, namely "FLT_EPSILON" and "DBL_EPSILON". These are the smallest number 'x' such that 1.0 + x != 1.0 for floats and doubles respectively.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Quote Originally Posted by joed
    What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

    Code:
    #include <math.h>
    
    int close(float a, float b)
    {
        if(fabsf(a - b) < .01)
            return 1;
        else
            return 0;
    }
    I do not want to compare values but perform calculations with them...

    Ex: 0.1 - 0.1 = 0 but 0.1 - 0.100009 <> 0 or does c treats them the same? I tried this:

    Code:
    float example_array[] = {0.2F, 0.1F};
    but it doesn't seem to work. I am a bit confused for the moment with all these data types...

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Ex: 0.1 - 0.1 = 0 but 0.1 - 0.100009 != 0
    That's right. If you want 110% kill-yer-mutha EXACT values you might consider writing your own base-10 number object

    Or you can do some rounding (maybe 2~3 digits) after calculations so the results seem "right".
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    http://en.wikipedia.org/wiki/Floating_point
    Until you understand what "approximate" means, floating point usage will always confuse you, and frustrate your attempts to write programs.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71

    Lightbulb

    You may consider using fraction numbers instead of floatingpoint.

    Code:
    typedef struct Fract {
        int     numerator;
        int     denominator;
    } Fract;
    
    #define FRACTS   2
    Fract    fract[ FRACTS ] = {
        { 2, 10 },
        { 1, 10 }
    };
    Or a class Fract, etc. There shall be plenty of examples and code on the net to Add, Sub, Mul and Div fractions, etc. It's an alternative to floating point arithmetics and it is exact. For example 1/3 can be represented exactly { 1, 3 } while a float or double will truncate it to something relatively close to 1/3 but not exactly 1/3, etc.
    You can also use a combination of fractions and floats! Use fractions during computings and convert the result to a float at the very end. That way you may get better precision in the computations. The drawback will be that it naturally takes longer time to compute fraction numbers if there is no direct hardware support, like an FPU helps computing floating point arithmetics.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Good idea buddy.
    Woop?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Thanks for the reply's, helped a lot! I'm sure I can make my program work now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 09:05 PM
  3. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  4. My attempt at lighting math
    By psychopath in forum Game Programming
    Replies: 11
    Last Post: 03-30-2005, 12:35 AM
  5. Backdooring Instantaneous Radius of Curvature & Functions
    By just2peachy in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2004, 01:25 PM

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