Why does this evaluate differently?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Why does this evaluate differently?

Post by Mistrel »

I don't understand this at all.

Code: Select all

n=1
q=n/3
Debug Pow(10,q) ; 1.0

Debug Pow(10,(n/3)) ; 2.1544346900318838
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Why does this evaluate differently?

Post by tinman »

Mistrel wrote:I don't understand this at all.

Code: Select all

n=1
q=n/3
Debug Pow(10,q) ; 1.0

Debug Pow(10,(n/3)) ; 2.1544346900318838
Because the compiler will promote the expression in the second one to a float or double you will get 10^0.3333.... You have already performed the n/3 calculation and stored it in the long variable in the first case and x^0=1.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

But can you explain this instead:
n=1
q.f=n/3
Debug Pow(10,q) ; 1.0

Debug Pow(10,(n/3)) ; 2.1544346900318838
I get:
2.1544347393126988
2.1544346900318838
Is it simply truncated in q.f or ???

I see the anwers to the 7th decimal place are the same. That is indeed enough accuracy for anyone in the aerospace industry.

Hmmm let me throw a few more debugs in.
Last edited by Rook Zimbabwe on Tue Mar 04, 2008 4:01 pm, edited 1 time in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

What do you mean? Do you wonder why it isn't 1.0 or why the numbers aren't equal?

Code: Select all

n=1 
q.d=n/3 
debug q
Debug Pow(10,q) ; 2.1544346900318838 

Debug Pow(10,(n/3)) ; 2.1544346900318838
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Why the numbers are not more equal.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Code: Select all

n=1 
q.d=n/3 
Debug q
Debug Pow(10,q) ; 1.0 

Debug Pow(10,(n/3)) ; 2.1544346900318838
q.d, NOT q.f... .f has limitations (Silly me!)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Last edited by hellhound66 on Wed Mar 19, 2008 11:31 pm, edited 1 time in total.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

OK I will give you that in standard precision they were indeed equal, but they were not exact. 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

a Float and even a Double almost never give an exact value.
or more precise:
for each value that can be stored 100% exactly, there are thousands of values that cannot.
that is immanent to the nature of floating-point storage.
Prof.Kahan wrote:13 Prevalent Misconceptions about Floating-Point Arithmetic :
1• Floating–point numbers are all at least slightly uncertain.
2• In floating–point arithmetic, every number is a “ Stand–In ” for all numbers that differ from it in
digits beyond the last digit stored, so “ 3 ” and “ 3.0 E0 ” and “ 3.0 D0 ” are all slightly different.
3• Arithmetic much more precise than the data it operates upon is needless, and wasteful.
4• In floating–point arithmetic nothing is ever exactly 0 ; but if it is, no useful purpose is served by
distinguishing +0 from -0 . ( We have already seen on pp. 13 - 15 why this might be wrong.)
5• Subtractive cancellation always causes numerical inaccuracy, or is the only cause of it.
6• A singularity always degrades accuracy when data approach it, so “ Ill–Conditioned ” data or problems
deserve inaccurate results.
7• Classical formulas taught in school and found in handbooks and software must have passed the
Test of Time, not merely withstood it.
8• Progress is inevitable: When better formulas are found, they supplant the worse.
9• Modern “ Backward Error-Analysis ” explains all error, or excuses it.
10• Algorithms known to be “ Numerically Unstable ” should never be used.
11• Bad results are the fault of bad data or bad programmers, never bad programming language design.
12• Most features of IEEE Floating-Point Standard 754 are too arcane to matter to most programmers.
13• “ ‘ Beauty is truth, truth beauty.’ — that is all ye know on earth, and all ye need to know.” ... from
Keats’ Ode on a Grecian Urn . ( In other words, you needn’t sweat over ugly details.)
oh... and have a nice day.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I forgot that I was casting to an integer. Oops! :shock:
Post Reply