# Failure contexts and the Floor() function

**URL:** <https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [April 30, 2023, 3:02am UTC](https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240 "2023-04-30T03:02:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MegaFatDog](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MegaFatDog](https://forums.unrealengine.com/u/MegaFatDog)\
**Post date:** [April 30, 2023, 3:02am UTC](https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240/1 "2023-04-30T03:02:56Z")

</div>

I have a four digit integer. I want to separate each digit out into it’s own variable. So instead of x = ‘1234’ I have x1 = ‘1’, x2 = ‘2’, x3 = ‘3’, x4 = ‘4’. My idea (non-Verse, just conceptually) is something like this for x1:

```auto
Given x = 1234
x_float = float(x)
x1 = int(Floor(x_float/1000))

```

I can cast the int to a float by multiplying it by 1.0 (is there a better way?) and the Floor function should return an int, so my question pertains to the Floor() function itself. My first crack at the function was this:

```auto
    DecodeTrackerCurrentValue(TrackerValue : int) : int =
        var TrackerFloatValue : float = TrackerValue*1.0
        Digit1 := Floor(TrackerFloatValue/1000.0)
        return Digit1

```

VSCode shows an issue on `TrackerFloatValue/1000.0`: ‘Function is invoked with incorrect failure bracketing style.(3511)’ This, I gather, is due to the fact that Floor() is a `<decides>` function and needs to be wrapped in a failure context. It makes sense to me why this is (since someone could input the wrong data type for instance) but what’s eluding me is _how._ I’ve been playing around with something like this:

```auto
        if (# Some check?):
            Digit1 := Floor(TrackerFloatValue/1000.0)

```

But I can’t figure out what to put in the parentheses or if this approach is even correct to begin with. Can someone help me understand what I’m trying to do here?

[Edit] Moved to Verse forum

---

<div class="post-metadata">

**Author:** ![meatandmeat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@meatandmeat](https://forums.unrealengine.com/u/meatandmeat)\
**Post date:** [April 30, 2023, 2:44pm UTC](https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240/2 "2023-04-30T14:44:32Z")

</div>

You have to call failable functions like Floor with [] instead of ()  
Then you can choose how you want to handle the failure

Option 1: Return a default value if Floor fails

```auto
if(Digit1 := Floor[TrackerFloatValue/1000.0]):
    return Digit1
else:
    return 0

```

Option 2: Return a ?int instead of int

```auto
if(Digit1 := Floor[TrackerFloatValue/1000.0]):
    return option{digit1}
else:
    return false?

```

---

<div class="post-metadata">

**Author:** ![MegaFatDog](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MegaFatDog](https://forums.unrealengine.com/u/MegaFatDog)\
**Post date:** [April 30, 2023, 4:22pm UTC](https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240/3 "2023-04-30T16:22:37Z")

</div>

Excellent, that makes things a lot more clear to me. Thanks!

---

<div class="post-metadata">

**Author:** ![AndrewScheidecker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andrewscheidecker/32/926976_2.png) [@AndrewScheidecker](https://forums.unrealengine.com/u/AndrewScheidecker)\
**Post date:** [May 6, 2023, 12:28pm UTC](https://forums.unrealengine.com/t/failure-contexts-and-the-floor-function/1149240/4 "2023-05-06T12:28:28Z")

</div>

You can factor an integer into digits with integer division and the integer Mod function. When using integer division, you get a rational number (type `rational`), which you must use the rational Floor function to get an integer out of.

Here’s a function that returns an array of digits (ordered from most significant to least significant):

```auto
GetDigits(I:int)<computes>:[]int=
    if(I=0) then array{}
    else if(Digit := Mod[I, 10]; Residual := Floor(I / 10)):
        GetDigits(Residual) + array{Digit}
    else:
        # This should never be reached because Mod and integer division only fail when the
        # divisor is zero. Nevertheless, the compiler doesn't know that, so this else is needed.
        Err("unreachable")

```

For example, `GetDigits(12345)=array{1, 2, 3, 4, 5}`.
