How to show this math equation?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

How to show this math equation?

Post by Dude »

Just saw this thread on Reddit:

https://www.reddit.com/r/software/comme ... alculator/

They want to get this math equation calculated so that it shows the result of 0.99999...

Code: Select all

(9460730472580800 * 4.3) / (((9460730472580800 * 4.3) ^ 2 + (.0617/2) ^ 2) ^ .5)
I'm hopeless at maths, so does someone want to try to convert it to PureBasic? :twisted:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to show this math equation?

Post by wilbert »

I think it should be converted like this

Code: Select all

result.d = (9460730472580800 * 4.3) / Pow(Pow(9460730472580800 * 4.3, 2) + Pow(0.0617/2, 2), 0.5)
Debug result
but PB shows 1.0 as the answer.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: How to show this math equation?

Post by VB6_to_PBx »

likewise VB6.0 shows 1.0000000000 as the answer

i can't get 0.99999 in PB or VB6 ??
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to show this math equation?

Post by STARGÅTE »

Its not possible in Floats or Doubles!

(9460730472580800 * 4.3)^2 has a magnitude of 33 and you will add a number (.0617/2)^2 with a magnitue of -3
That means, you need a number format with a precision of 36 decimal places (~120 bit).
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: How to show this math equation?

Post by bbanelli »

"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: How to show this math equation?

Post by Michael Vogel »

As Stargate said, you would need a higher precision to calculate a different value than 1.0 for the term:

40681141032097440 / 40681141032097440.0000000000000000000001169734274720...

You woudn't even be able to assign the correct result to a variable in Purebasic:

result.d=0.999999999999999999999999999999999999712462766519257627;...
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: How to show this math equation?

Post by JHPJHP »

Hi All,

I am sure most of you are aware of WolframAlpha.

Code: Select all

(9460730472580800 * 4.3) / (((9460730472580800 * 4.3) ^ 2 + (.0617/2) ^ 2) ^ .5)
From the above link paste the equation into the input field, press enter, and wait for the process to finish. Next press the More digits button.
- each time the More digits button is pressed, the greater the precision

-----------------------------------------------

WolframAlpha also offers a (mostly) free API: Limit of 2000 free calls per month.
- Developer Portal: Create Account / Sign In
- Online Documentation
- WolframAlpha-API-Reference.pdf

I have a registered account. The included examples are for demo purposes only; please get your own AppId.
- Services, Stuff & Shellhook
-- \Stuff\WolframAlpha\WolframAlpha_1.pb
-- \Stuff\WolframAlpha\WolframAlpha_2.pb
Last edited by JHPJHP on Wed May 23, 2018 3:24 pm, edited 1 time in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to show this math equation?

Post by Dude »

We have a winner! :D Thanks JHPJHP. Good to see that we can query Wolfram like this. I kept your second example for this thread, but removed your AppID from it.

Code: Select all

#HighPrecisionEquation  = "(9460730472580800 * 4.3) / (((9460730472580800 * 4.3) ^ 2 + (.0617/2) ^ 2) ^ .5)"
#MoreDigitsLevel        = "2"

URL.s                   = "https://api.wolframalpha.com/v2/query"
AppId.s                 = "?appid=XXXXXX-XXXXXXXXXX"
Input.s                 = "&input=" + URLEncoder(#HighPrecisionEquation)
PodState.s              = "&podstate=" + #MoreDigitsLevel + "@Result__More+digits"
Output.s                = "&output=JSON"
InitNetwork()
Debug PeekS(ReceiveHTTPMemory(URL + AppId + Input + PodState + Output), -1, #PB_UTF8)
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: How to show this math equation?

Post by Michael Vogel »

When precise calculations have do be done offline, you may want to use the Gnu MP library (see here).

Here's the needed code for the calculation:

Code: Select all

#Precision=20000

Procedure.s Zahl(Adresse,len=#Precision)
	Protected s.s=Space(len)
	gmp_snprintf(s,len,"%Z."+Str(len)+"Ff",Adresse)
	ProcedureReturn s
EndProcedure
Procedure Dude()
	
	
	mpf_set_default_prec(#Precision)

	x.mpf
	y.mpf
	z.mpf
	a.mpf
	b.mpf
	c.mpf
	s.s

	mpf_init2(@x,#Precision)
	mpf_init2(@y,#Precision)
	mpf_init2(@z,#Precision)
	mpf_init2(@a,#Precision)
	mpf_init2(@b,#Precision)
	mpf_init2(@c,#Precision)
	
	mpf_set_str(@a,"9460730472580800",10)
	mpf_set_str(@b,"4.3",10)
	mpf_set_str(@c,"0.617",10)
	
	mpf_mul(@x,@a,@b)
	mpf_pow_ui(@y,@x,2)
	mpf_div_ui(@z,@c,2)
	mpf_pow_ui(@z,@z,2)
	mpf_add(@y,@y,@z)
	mpf_sqrt(@y,@y)
	
	mpf_div(@x,@x,@y)
		
	Debug zahl(@x,#Precision/10)
	
	mpf_clear(@x)
	mpf_clear(@y)
	mpf_clear(@z)
	mpf_clear(@a)
	mpf_clear(@b)
	mpf_clear(@c)
	
EndProcedure
Post Reply