• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

132.5 --> 0.5?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
If you just want the original real number to be any other real number between 0.00 and 1.00, then you can do 1 divided by your number e.g 1/132.5

No I want to keep the numbers after the decimal point. Made this small function:

JASS:
private function GetDecimal takes real n returns real
    local string s = R2S(n)
    local integer i = StringLength(s) 
    return S2R(SubString(s, i -4, i))
endfunction
 
Level 12
Joined
Mar 13, 2012
Messages
1,121
How about this?
JASS:
function HasFractional takes real r returns boolean
    return r!=R2I(r)
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
JASS:
function HasFractional takes real r returns boolean
     return r!=R2I(r)
endfunction
Might return false-positive for 0. Floating point can have both a positive and negative 0 value which when compared I recall returning false for equality. I know they are meant to return true as they represent the same numeric value however it all depends on the implementation used.

JASS:
private function GetDecimal takes real n returns real
     return (n - I2R(R2I(n)))
endfunction
This might be prone to breaking for large numbers due to floating point error.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
since real can in Jass store only one tenth of int, it shouldnt really, and yeah, people shouldnt use 10+ order reals, cause that doesnt make sense in Jass really.

Still, my implementation is the simplest and correctest presented, and I dont think it can boil down any lower, since I perform 1 math operation and 2 native calls
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
//===========================================================================
// Calculate the modulus/remainder of (dividend) divided by (divisor).
// Examples:  13.000 mod 2.500 = 0.500.  -6.000 mod 2.500 = 1.500.
//
function ModuloReal takes real dividend, real divisor returns real
    local real modulus = dividend - I2R(R2I(dividend / divisor)) * divisor

    // If the dividend was negative, the above modulus calculation will
    // be negative, but within (-divisor..0).  We can add (divisor) to
    // shift this result into the desired range of (0..divisor).
    if (modulus < 0) then
        set modulus = modulus + divisor
    endif

    return modulus
endfunction

vs mine is still a lot less effective and it does basically the same thing(convert to int, convert back to real), so why the extra unneeded work around it
 
Status
Not open for further replies.
Top