Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Fractional and Integer part

gianx80gianx80 Member Posts: 7
Hi to all!
I'm writing an assembly program that at a certain point calculates a square root, then it has to verify if the result is an integer (i.e. 2.000) or a "true" floating point number (i.e. 2.456).
How can I do this in Assembly language?

P.S.
The next week I have to take an exam about this kind of things, I hope you can help me.

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Do you use FPU to calculate the root? FPU has the rounding mode called ROUND WITH TRUNCATE - it will give the whole part of the value. Then you subtract this from the value and get the fractional part - then check it for zero. However, because of precision issues - that part may not be entirely zero.

    Example: you calculate root of value 4 and as a result of the algorithm you get back the value of 2.00000000000783. Then, by performing the TRUNCATE - you get back the fractional part as: 0.00000000000783 - now you need to check it for zero and the check will say: NOT ZERO. Advice: you need to check for zero using some kind of range, like [-.000000000000005 .. +.000000000000005] and if your fractional part is in that range - then you got a zero fractional part and your result is a whole value.

    With all that said - in reality it is impossible to resolve such situation because of FPU final precision - it has 80 bits to operate on and it is always possible to lose bits during calculations.[/color]
  • m34tb34tm34tb34t Member Posts: 25
    [code]
    ;this doesn't use the fpu but is still quite slow
    someFunction:
    xor ax,ax
    tryAgain:
    mov bx,ax
    inc ax
    push ax
    xor dx,dx
    mul ax
    cmp ax,value ; your value (pre-square root)
    pop ax
    jb tryAgain
    je perfectMatch
    ;here AX*AX > value > BX*BX
    ;in other words: AX > sqrt(value) > BX
    ;and AX = BX + 1
    ;therefore root is non-integer "true" floating point
    ret
    perfectMatch:
    ;AX = root (which is an integer)
    ret
    [/code]

    "The next week I have to take an exam about this"

    Sorry I was late... hope you did well on it...
Sign In or Register to comment.