#### mabdelouahab

• Member
• Posts: 403
##### FPU - Adjust the result
« on: October 03, 2018, 01:30:57 AM »
Why not be the exact result?
Code: [Select]
` FLD FP8(6.0) FSQRT FMUL FP8(3.0) FSTP a FLD FP8(54.0) FSQRT FSUB a FSTP b`==0.0000000000000009

#### mabdelouahab

• Member
• Posts: 403
##### Re: FPU - Adjust the result
« Reply #1 on: October 03, 2018, 01:52:20 AM »
sqrt(2-sqrt(2))/sin(Π/8) = 2
but:
Code: [Select]
` FLD FP8(2.0) FSQRT FSTP a FLD FP8(2.0) FSUB a FSQRT FSTP a ;a=sqrt(2-sqrt(2)) FLDPI FDIV FP8(8.0) FSIN FSTP b ;b=sin(Π/8) FLD a FDIV b FSTP b ;b=sqrt(2-sqrt(2))/sin(Π/8)`b=        1.9999999999999998

#### jj2007

• Member
• Posts: 9472
• Assembler is fun ;-)
##### Re: FPU - Adjust the result
« Reply #2 on: October 03, 2018, 02:55:38 AM »
What's your rounding mode? I get exactly zero (and exactly 2.0 for the second example):
Code: [Select]
`.data?a REAL10 ?b REAL8 ?.code  FLD FP8(6.0)  FSQRT  FMUL FP8(3.0)  FSTP a  FLD FP8(54.0)  FSQRT  fld a  FSUB  FSTP b  printf("Result=%f", b)`

#### RuiLoureiro

• Member
• Posts: 819
##### Re: FPU - Adjust the result
« Reply #3 on: October 03, 2018, 03:12:30 AM »
What's your rounding mode? I get exactly zero (and exactly 2.0 for the second example):
Code: [Select]
`.data?a REAL10 ?b REAL8 ?.code  FLD FP8(6.0)  FSQRT  FMUL FP8(3.0)  FSTP a  FLD FP8(54.0)  FSQRT  fld a  FSUB  FSTP b  printf("Result=%f", b)`
Jochen, i get exactly the same in TheCalculator (0 and 0 and 2.0). TheCalculator uses REAL10 format and the converter uses FPU.

LATER: it seems that the calculator above uses a converter that shows garbage ( as noted by Siekmanski in his topic about his real4 to ASCII converter, but i dont know ).
« Last Edit: October 03, 2018, 07:09:10 AM by RuiLoureiro »

#### HSE

• Member
• Posts: 1017
• <AMD>< 7-32>
##### Re: FPU - Adjust the result
« Reply #4 on: October 03, 2018, 03:32:22 AM »
Why not be the exact result?
Code: [Select]
` FLD FP8(6.0) FSQRT FMUL FP8(3.0) FSTP a FLD FP8(54.0) FSQRT FSUB a FSTP b`==0.0000000000000009
This is "sqrt(54) - sqrt(6)*3"

LATER:
Code: [Select]
`-0.00000000000000089Result=-0.0000001.99999999999999980Result=2.000000Press any key to continue ...`
The ugly number it's what really is stored in FPU

#### mabdelouahab

• Member
• Posts: 403
##### Re: FPU - Adjust the result
« Reply #5 on: October 03, 2018, 04:16:50 AM »
What's your rounding mode? I get exactly zero (and exactly 2.0 for the second example):

I'm retesting in  down mode  and truncate mode so I got the result exact, but in mode nearest and mode up I get same result

#### HSE

• Member
• Posts: 1017
• <AMD>< 7-32>
##### Re: FPU - Adjust the result
« Reply #6 on: October 03, 2018, 04:31:55 AM »
Hi mabdelouahab!

I got the result exact

You can't expect to obtain exact results using FPU, because FPU doesn't make mathematics, only very close numerical approximations.  :t

#### raymond

• Member
• Posts: 233
##### Re: FPU - Adjust the result
« Reply #7 on: October 04, 2018, 11:00:12 AM »
The square root of a number which is not a perfect square can only be obtained to whatever precision is obtainable by either the hardware or the algo you are using. The best precision you can expect from 128-bit hardware would be the 128th bit. Depending on your rounding mode, that last bit would have been rounded according to the 129th bit if it had been possible to 'keep' it.

In 'down' and 'truncate' modes, the value of that 129th bit is totally disregarded but could be significant in the 'nearesr' mode. The reported difference would then be that 129th bit which has absolutely NO significance when converted to ascii, whether you display it with 1 or 100 decimal digits. 2-128 is approximately equal to 10-39.
Whenever you assume something, you risk being wrong half the time.
http://www.ray.masmcode.com/