to all math genius!

This is a discussion on to all math genius! within the C++ Programming forums, part of the General Programming Boards category; i want to update a value whcih should be not greater than 1 and the updated value should also be ...

  1. #1
    Mathy
    Guest

    to all math genius!

    i want to update a value whcih should be not greater than 1 and the updated value should also be not greater than 1.if the given value is 0.9999 then the updated value should be 0.99999 but i dont know that how many 9s will be there in the given value. so ne ideas?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I certainly did not understand your question but maybe someone else can help out.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    So you want a variable that's under 1 to increase but to stay
    under 1, That's easy, Use floats.

    Code:
    float number=0.5;
    
    while(1)
    {
    
    if(number<0.999999999)
    {
           number+=0.000000001 // Don't know exactly how large floats can get
    }
    else
    {
       break;
    }
    
     cout << number;
    
    }

  4. #4
    kohatian3279
    Guest
    but still it is setting restriction on the input number so that it should be less than 0.999999999, what abt 0.99999999999999999999999? it should also accept that and update it with one more .000...9. got the idea?

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    in the past, I've used .0000001 or some variety as a "distance from a number"

    for instance if you want to find out if n is approx 5 you can do this...

    isapprox = fabs(n-5)<.0000001;
    //fabs is of course absolute value of a float

    I hope that helps.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    float IncreaseValue(float Value)
    {
       float Increaser = 1.0 - Value;
       Increaser /= 10;
       return (1.0 - Increaser);
    }
    If Value is 0.99999, Increaser will be 1.0 - 0.99999 = 0.00001.
    Increaser divided by 10 will give you 0.00001 / 10 = 0.000001.
    Subtract Increaser from 1.0 and you will get 1.0 - 0.000001 = 0.999999, which is greater than 0.99999 but still less than 1.

    Note that floats aren't always 100% exact so you might get some irregularities.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ya, there's a definite precision issue with floating point numbers. the .9999999 works to a degree of precision.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Kohatian3279
    Guest

    i got it!

    x=1-value;
    y=x*0.9;
    value=value+y

  9. #9
    stovellp
    Guest
    You could always make an array of INT's of a dynamic size so that you can just keep adding them on once one reaches 9.

    eg:
    [code]
    if (myArray[X] == 9)

  10. #10
    stovellp
    Guest
    You could always make an array of INT's of a dynamic size so that you can just keep adding them on once one reaches 9.

    eg:
    [code]
    if (myArray[X] == 9)
    {
    myArray[++X] = new int;
    myArray[X] = 1;
    }

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Am I the only one that hates pre/post incremented/decremented variables in the middle of an expression?

    Is that a hardware supported feature? (I know some of them exists in Motorola processors) If it isn't, then it's one helluwa unneccessary way of complicating code...
    Last edited by Magos; 02-06-2003 at 04:50 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    stovellp
    Guest
    Well I could have just written a whole extra statement saying X++; but then I remembered, theres no difference, and I'm slack, and my teacher won't be reading it.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Sometimes itīs nice to use pre/post increment/decrement, when an extra line of code seems unnecesary.

    Example:

    Code:
    int temp,index;
    int array[BIG];
    
    [...somewhere inside a loop]
    
    temp = array[index++];
    In this case itīs obviously whatīs going on.

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Hardboy
    In this case itīs obviously whatīs going on.
    Perhaps to those who know the functionality of pre/post decremented operators, but just looking at the expression an assignment and a variable increment are done on the same line. You can't tell which one is executed first. And it does matter which order they are.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    22
    The key is to create a power function, check the closeness to one, and then at 10 to the -whatever, depending on how many didits are past the decimal point.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 03:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 09:06 PM

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