QB64 Phoenix Edition
Any math experts know what I'm missing here? [Solved] - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Any math experts know what I'm missing here? [Solved] (/showthread.php?tid=911)

Pages: 1 2 3


RE: Any math experts know what I missing here? - Jack - 09-23-2022

Pete, have you seen this? https://www.wikihow.com/Calculate-Cube-Root-by-Hand


RE: Any math experts know what I missing here? - Pete - 09-23-2022

@Jack

I did read that yesterday. That plus this https://en.wikipedia.org/wiki/Nth_root#Examples on Wikipedia helped me make this, which I think works... [NOTE: I haven't put a decimal place in yet, just digits.]

Code: (Select All)
$CONSOLE:ONLY
DIM AS DOUBLE d, i, j, r, t, x, oldx
INPUT "Whole number: "; n%
INPUT "Root: "; r%
r = r%
n$ = LTRIM$(STR$(n%))
h&& = (r - (r - LEN(n$) MOD r)) + 1
t = VAL(MID$(n$, 1, h&& - 1))
limit&& = 8
DO
    lcnt&& = lcnt&& + 1
    PRINT "Target ="; t
    FOR j = 1 TO 10
        x = 0
        FOR i = 1 TO r
            IF i = 1 THEN
                PRINT "(10 ^"; (i - 1); "*"; i; "* d ^"; i - 1; " * j ^"; (r + 1 - i); ") + ";
                x = x + 10 ^ (i - 1) * i * d ^ (i - 1) * j ^ (r + 1 - i)
            ELSE
                PRINT "(10 ^"; (i - 1); "*"; r; "* d ^"; i - 1; " * j ^"; (r + 1 - i); ") + ";
                x = x + 10 ^ (i - 1) * r * d ^ (i - 1) * j ^ (r + 1 - i)
            END IF
        NEXT
        PRINT "<<<"; x
        IF x > t THEN EXIT FOR
        oldx = x
    NEXT
    d = VAL(LTRIM$(STR$(d)) + LTRIM$(STR$(j - 1)))
    PRINT "t - oldx as: "; t; "-"; oldx; "="; t - oldx,: COLOR 14: PRINT "d ="; d: COLOR 7
    tmp1$ = LTRIM$(STR$(t - oldx))
    tmp2$ = MID$(n$, h&&, r) + STRING$(r - LEN(MID$(n$, h&&, r)), "0")
    PRINT "tmp1$ = "; tmp1$, "tmp2$ = "; tmp2$, "n$ = "; n$
    t = VAL(tmp1$ + tmp2$) ' This will remove any leading zeros carried down.
    h&& = h&& + r
    IF t = 0 AND h&& >= LEN(n$) OR lcnt&& = limit&& THEN EXIT DO
LOOP
PRINT
RUN

Pete


RE: Any math experts know what I missing here? - Kernelpanic - 09-23-2022

Quote:Let's say, and I'll just make up a number for this: 4.9665242234315151119541094114141414143678 and it is supposed to be 5.


The example with the number 4.9665 ... which is supposed to be 5 reminded me that I once had to solve a similar problem myself.
A good 20 years ago I wrote an article in a Java magazine about a financial calculation. During programming I had to find out that the corresponding function in Java 1.1 had a nasty bug. If there was a 9 at the third decimal place, something like this: 4,869... then it was not rounded up, but 4,869 became 4.1. Very evil, and very nasty! I only recognized the error because I had previously programmed the same stuff in C.

OK! In Java there was no way to separate the decimal part like in C with fmod(). The only way was to convert the number to a string, check the decimal part, and then convert everything back to a number. See the screenshot.

You can do the same here too. Pseudocode:
Exponent (n) is 5. If the decimal part is 9 in the first place and greater than 5 in the third place, then n = 5. Why still iterations?


[Image: Zahlen-zu-Strings.jpg]


RE: Any math experts know what I missing here? - Pete - 09-23-2022

Well that's rounding for you, and rounding is tricky. Let's say I want the cube root of 7.997600239992. That is approximately 1.998. Rounding that as 2 would be an error. So then we start to require rules like: Do not round to a whole number if the number the root is extracted from is a decimal, etc., etc., etc.. I'm not sure who wrote the bible on rounding exceptions, but I'm not interested in becoming a disciple.

Pete


RE: Any math experts know what I missing here? - Kernelpanic - 09-23-2022

(09-23-2022, 08:45 PM)Pete Wrote: Well that's rounding for you, and rounding is tricky. Let's say I want the cube root of 7.997600239992. That is approximately 1.998. Rounding that as 2 would be an error. So then we start to require rules like: Do not round to a whole number if the number the root is extracted from is a decimal, etc., etc., etc.. I'm not sure who wrote the bible on rounding exceptions, but I'm not interested in becoming a disciple.

Pete

Well Pete, it's your path you want to go!


RE: Any math experts know what I missing here? [Solved] - Pete - 09-23-2022

The path doesn't bother me. It's the damn rabbit holes along the path that bothers me.

Here's a beauty...

For calculators it is easy enough to do fraction division, and express the result as a decimal. I just keep the fraction for the next calculation. Now with roots, I don't see a way to do that. Roots often result in very long or non-terminating decimals. The long division binomial expansion method I just put together does not appear to have any divisor to place a remainder over. So if that's so, and I want to use roots in my string math calculator, this would totally blow apart the accuracy of using fractions in division when a root extraction cannot be expressed as a fraction and blows all that work straight down the rabbit hole.

Pete


RE: Any math experts know what I missing here? [Solved] - Kernelpanic - 09-25-2022

I've now created this in Visual Basic as well, and if you compare that to the output from QB64, the result is correct. But maybe one need 100 digits after the decimal point in atomic physics. I don´t know.  Rolleyes

[Image: nte-Wurzel-in-QB64-VB6.jpg]


RE: Any math experts know what I'm missing here? [Solved] - Pete - 09-26-2022

Like cube root of 19 to 100 places?

2.668401648721944867339627371970830335095878569183101865664213586945793971672059716316681593100516540

or how about 1,000?

2.668401648721944867339627371970830335095878569183101865664213586945793971672059716316681593100516540971200115121674125395007158654306951149574674557937819247978231238324074345969306769527367552077588789283162098019865860310973250749366855816387636411912497141957497075505870839424737202877377620335424466759417959346644389526318379034086274805653954347616576587643586016635041559445622377433338136015881058842865546315105639472400872468056996811327861031078857024674652824263716248095765210682549755520817960718459266515565633668331455418178360503783875980933641063457961841915715468558854504732505707270369593102468158975185730051221532488066929421076424593422434976561310400347154900112534271679002569726757164211489255334200509328542492626608085317755137411050512911632210102102304034256017180280147241524905629502377797452960048708915324624653820450799494643975595244219506033096681932949861574867095932178514065043374071529672321318975592408317607496760349497350851789545929864955777170940718277

The hundred output took less than a second, but the 1000 output took nearly 3-minutes. I would need to figure out an estimate program to cut that down to around 15-30 seconds.

Pete


RE: Any math experts know what I'm missing here? [Solved] - bplus - 09-26-2022

@Pete, do you have decimal place worked out for Square Roots?

I just modified an Extended Integer Square Root code to a Real Square Root in String Math. I wonderful improvement over the old estimate method I had, faster and accurate to any amount of decimals!


RE: Any math experts know what I'm missing here? [Solved] - Pete - 09-26-2022

I do have the decimal places worked out for square roots. I need to either modify it for nth roots, or see if I need to tweak it for square roots, in case I missed something. One I settle on a working method, I'll probably scrap the square root one and just use the nth root routine.

Looking forward to getting this up as a "Work in Progress" soon. Right now, it has been fun learning, and putting together systems. While approximation methods are fun, they are just not practical, and too slow for very large numbers. Long division is accurate. Long division with a way to approximate the next part of the quotient is also fast. I'm not quite sure how to do that estimate with nth roots, yet. I have already incorporated a great estimate method in the standard long division process. I hope to apply something similar to nth roots, soon. 100 digits of a two-digit number cube root takes under 1-second, but it takes a 2-3 minutes to take that same two-digit number out to 1,000. places

Anyway, here is a link to my latest nth root in string math, as well as a little routine to calculate the values in Pascal's Triangle.

https://qb64phoenix.com/forum/showthread.php?tid=918

Will you be posting your modified extended square root code? Fun stuff!

Pete