PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 [Solved]Calculations giving Infinity?-VBForums
Results 1 to 10 of 10

Thread: [Solved]Calculations giving Infinity?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    43

    [Solved]Calculations giving Infinity?

    Hi,

    I have to use some calculations now and then and sometimes those will give me inf, and sometimes they work, this makes my app very inaccurate...

    Now i dont know what is causing this, im not dividing zero so far i can see..
    im not realy a math person so i thought, maybe someone in here knows a solution.

    ill give an example, When the calculation would be:

    Code:
    (3 + Math.Sqrt(5)) ^ ...
    It works for single numbers, But how would i retrieve the ?double? of bigger numbers without getting INF?
    With bigger numbers i mean really big, like 1477014138.

    I hope someone could help me understanding this.

    Regards,
    Shinigami
    Last edited by Shinigami; Sep 11th, 2009 at 01:35 AM. Reason: Solved

  2. #2
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Calculations giving Infinity?

    If you are going to retrieve numbers that holds decimal numbers, than you should dim the variable holding the product as a double.

    vb.net Code:
    1. Sub Main()
    2.         Dim result As Double = Math.Sqrt(15)
    3.         'The result is 3.8729...
    4.         Console.WriteLine(CStr(result))
    5.         Console.ReadKey()
    6.     End Sub
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    43

    Re: Calculations giving Infinity?

    Thats what i have now (well, simplified, but sort of) now try this and you will see what i mean.

    Code:
    Sub Main()
    Dim result As Double = (3 + Math.Sqrt(5)) ^ 1477014138
    Console.WriteLine(CStr(result))        
    Console.ReadKey()   
    End Sub
    Regards,
    Shinigami

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Calculations giving Infinity?

    Do you even realize how big that number is? There is no data type in .NET that can hold a value that big.

    Even google will only let you do up to (5.23606798 ^ 428) = 5.42323329 × 10E+307, which just so happens to be the double types max before it hits infinity.

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Calculations giving Infinity?

    How big is your Console? I'm asking because the number you're trying to compute is approximately 1.5286 10^1061984881, which consists of more than 1 billion digits.
    In fact, it's not even physically possible to display that number in your console, or any kind of display, since there are not nearly enough atoms in the universe to make up the numbers, let alone a screen big enough to display it. There are only about 10^80 atoms in the observable universe, which is a number with 80 digits. Not quite in the same league, is it?

    That number is insanely large. Numeric data types have a maximum value they can hold. I don't know off the top of my head how much the Double can hold, but not quite as much as you are trying to put in there.

    I think there are custom types / classes that can hold any number, but they are likely to be extremely slow for such large numbers.


    What are you even trying to do here? Do you want to do some calculations with it? In fact, calculations with it are possible. Any decent math package can do it for you (in fact, I used Maple to calculate that (3+sqrt(5))^1477014138 = 1.5286 10^1061984881), but they sure don't use the exact value. They probably use some complicated maths to figure that out.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    43

    Re: Calculations giving Infinity?

    I know the whole number would be big, But i only need the last (accurate, as it must match the database) 3 numbers before the first "." it shouldnt be too hard as alot of people already did the practise exercise (its from Google code jam of last year.), i solved the first one using the simple method, but no clue about the 2nd one (with those big numbers).

    Any idea how to solve this?

    Regards,
    Shinigami

  7. #7

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    43

    Re: Calculations giving Infinity?

    Yes exacly, well the first file i solved by just calculating it then using split and right. :P
    2nd one's numbers are just to big to do it on that way as i get inf..

    [Edit]
    I got an C++ source here that does the trick, But i dont know C++ good enough to see an VB Equavant of it..

    Code:
    include <stdlib.h>
    #include <stdio.h>
    
    int mod = 1000;
    
    class matrix
    {
    public:
    	int data[2][2];
    };
    
    matrix multiply(matrix a, matrix b)
    {
    	matrix result;
    	result.data[0][0] = (a.data[0][0] * b.data[0][0] + a.data[0][1] * b.data[1][0]+ 1000000) &#37; mod;
    	result.data[0][1] = (a.data[0][0] * b.data[0][1] + a.data[0][1] * b.data[1][1]+ 1000000) % mod;
    	result.data[1][0] = (a.data[1][0] * b.data[0][0] + a.data[1][1] * b.data[1][0]+ 1000000) % mod;
    	result.data[1][1] = (a.data[1][0] * b.data[0][1] + a.data[1][1] * b.data[1][1]+ 1000000) % mod;
    	return result;
    }
    
    matrix power(matrix a, int n)
    {
    	matrix result;
    	result.data[0][0] = result.data[1][1] = 1;
    	result.data[0][1] = result.data[1][0] = 0;
    	matrix power = a;
    	for (int i = 0; i <= 30; i++)
    	{
    
    		if ((n & (1 << i)) != 0)
    		{
    //			printf("1");
    			result = multiply(result, power);
    		}
    //		else
    //			printf("0");
    		power = multiply(power, power);
    	}
    //	printf("\n");
    	return result;
    }
    
    void print(matrix a)
    {
    	printf("%d %d %d %d\n", a.data[0][0], a.data[0][1], a.data[1][0], a.data[1][1]);
    }
    
    int main()
    {
    	matrix trans;
    	trans.data[0][0] = 0;
    	trans.data[0][1] = 1;
    	trans.data[1][0] = -4;
    	trans.data[1][1] = 6;
    
    	int numCase, i, n;
    	scanf("%d", &numCase);
    	for (i = 0; i < numCase; i++)
    	{
    		scanf("%d", &n);
    		n--;
    		matrix tP = power(trans, n);
    //		printf("%d\n", n);
    //		print(trans);
    		int res = (tP.data[0][0] * 6 + tP.data[0][1] * 28+ 1000000) % mod;
    		if (res == 0) res = 999;
    		else res--;
    		printf("Case #%d: %03d\n", i+1, res);
    	}
    	return 0;
    }
    Can anyone put me a step into the right direction?

    Regards,
    Shinigami
    Last edited by Shinigami; Sep 10th, 2009 at 05:17 PM.

  9. #9
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Calculations giving Infinity?

    If I'm understanding you correctly, you are only required to know the hundreds?

    If that is the case, you only need ignore the left side of the number when it grows over 3 or 4 digits.

    Here's a very rough idea of how I would begin if I were in your shoes. I'm using 4 digits and am NOT using decimals. You can worry about all that fun stuff since it's your project.

    I'm not claiming that this code is as fast or as accurate as you need it to be. In fact, I can assure you this can be improved significantly. You would need to handle at least 5 decimal places to get a result that began to resemble accuracy, in my humble opinion. This is just an illustration of the concept that I am proposing.

    We will handle the exponents ourself and seperate each digit of the result, placing it in a piece of an array. We then join the array back together and run the calculation again.

    I used 10 as the exponent in this example because if you can get it to work with 10, you can get it to work with an octillion or whatever you can feed it.

    Code:
            Dim iExp As Int64 = 10 '1477014138
    
            Dim iX() As Int16 = {0, 0, 0, 0}
    
            Dim calcA As Double = 3 + Math.Sqrt(5)
    
            Dim result As Int64 = calcA
    
            For iPower = 0 To iExp - 1
                result = result * calcA
                iX(3) = Strings.Mid(result.ToString, Len(result.ToString), 1)
                If Len(result.ToString) > 1 Then iX(2) = Strings.Mid(result, Len(result.ToString) - 1, 1)
                If Len(result.ToString) > 2 Then iX(1) = Strings.Mid(result, Len(result.ToString) - 2, 1)
                If Len(result.ToString) > 3 Then iX(0) = Strings.Mid(result, Len(result.ToString) - 3, 1)
                result = iX(0) & iX(1) & iX(2) & iX(3)
            Next
    
            Console.WriteLine(result)
    Anyway, I hope this helps. It was fun to think about.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2009
    Posts
    43

    Re: Calculations giving Infinity?

    Thanks, That will give me a new method to think about hehe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width