#Why is this NaN?

82 messages · Page 1 of 1 (latest)

swift latch
#

This is my code and the log:

        let decayFactor = 0.99
        item.value.value = item.value.amount * Math.pow(decayFactor, average / 10)*(0.5+Math.random())
        console.log(itemName,item.value.value,item.value.amount,average)```
```[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: silicon
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: NaN
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 50.0
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 0.0d [net.minecraft.nbt.DoubleTag]
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: glass
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: NaN
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 250.0
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 0.0d [net.minecraft.nbt.DoubleTag]
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: lava_bucket
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: NaN
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 500.0
[18:37:13] [INFO] mystery_lands_mine_factory.js#2532: 0.0d [net.minecraft.nbt.DoubleTag]```
Taking those values and putting them into an online js envoironment, it works just fine and gives me a valid number(As seen in the screenshot)
nova radishBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

tight pine
swift latch
#

Wait what?

#

It works for you?

tight pine
#

yes

swift latch
#

Also how do you get those grey y: and x: ?

tight pine
#

this is vscode's setting

#

inlay hints

swift latch
#

Ah thanks a lot, theats really helpful

#

Its enabled, but I don't see them

tight pine
#

"javascript.inlayHints.parameterNames.enabled": "all"

swift latch
#

I tried logging the value, not the variable I set it too, still nan

swift latch
#

Thanks

swift latch
#

Did you do it in minecraft?

tight pine
#

yes

swift latch
#

Now thats weird

tight pine
#

try to log all step?

#
console.log(Math.pow(decayFactor, average / 10));
console.log((0.5 + Math.random()));
swift latch
#

Seams like this is nan Math.pow(decayFactor, average / 10)

#

Using 1 instead of the average variable it works fine

tight pine
#

now test average / 10

swift latch
#

Nan

tight pine
#

typeof average

swift latch
#

Object

#

Wait what?

tight pine
#

try Object.keys(average)

swift latch
#

[getAsFloat, getAsByte, getClass, wait, getAsNumber, notifyAll, getId, getAsString, asFloat, type, getAsInt, notify, asInt, getAsLong, hashCode, asShort, copy, id, asString, write, class, asByte, asNumber, getAsDouble, accept, asLong, getType, equals, acceptAsRoot, asDouble, toString, getAsShort, specialEquals] [java.util.ArrayList]Wtf is going on here

tight pine
#

It’s getting weirder

swift latch
#

True

tight pine
#

average.class

#

or averag["class"]

swift latch
#

[19:05:22] [INFO] mystery_lands_mine_factory.js#2532: class net.minecraft.nbt.DoubleTag [java.lang.Class]

tight pine
#

DoubleTag

#

so average from the item's nbt?

swift latch
#
        let average = 0
        history.forEach(price => {
        if(average <= 0) {
            average = price
        } else {
            average = (average+price)/2
        }
        })
        item.value.average = averageReal```Those are the calculations involving average
swift latch
tight pine
#

server persistant data is nbt too

#

try +average / 10

swift latch
#

Still nan

tight pine
#

It seems like implicit conversion doesn't work here

#

+average.toString(), +(average + "")

swift latch
#

Maybe convert the nbt to an int before adding it to average?

#

Its weird, I waork with persistant data A LOT and I never had this issue before

tight pine
#

Honestly, I don't know much about converting nbt to js objects.

swift latch
#

Ok, no problem

swift latch
#

I got no idea how to do that

#

Probe odesn't suggest anything

#

Can I go from a string to a number?

#

there is a methode for converting nbt to string

tight pine
#

+String

swift latch
#

Everything I try seams to give back NaN

#

I mean it seams like it is a double now, but I still get NaN when deviding it by 10

#

00.0d

swift latch
#

If anyone has an idea how to convert nbt to an int, please tell me

silk helm
#

@swift latch can you just console.log the average?

swift latch
silk helm
#

try average.getAsDouble()

tight pine
silk helm
#

DoubleTag not Java primitive double

tight pine
swift latch
silk helm
#

i bet you are trying to do some math but it is actually concatenating strings

#

check you code again where it involves average

#

00.0d is not normal

#

looks like it is "00.0d"

#

a string

swift latch
#

full code: js let machineData = e.server.persistentData.machineDetails[player.getUuid()] storedItems.forEach(itemName => { let item = machineData.storage[itemName] let history = item.value.history history.push(item.value.thisTick) if(history.length > 10) { history.shift() } item.value.thisTick = 0 let average = 0 let averageReal = 0 history.forEach(price => { averageReal += price if(average <= 0) { average += price } else { average = (average+price)/2 } }) averageReal = averageReal / history.length item.value.average = averageReal let decayFactor = 0.99 item.value.value = item.value.amount * Math.pow(decayFactor, average / 10)*(0.5+Math.random()) console.log(itemName,item.value.value,item.value.amount,average,average / 10) })

silk helm
#

average += price
0 + "0.0d"
= "00.0d"

prob this is happening

swift latch
#

But how do I make it not a string

#

üutting + before it doesn't work

silk helm
#

price.getAsDouble()

#

also here averageReal += price

swift latch
#

The console now prints out this: [07:47:37] [INFO] mystery_lands_mine_factory.js#2532: lava_bucket [07:47:37] [INFO] mystery_lands_mine_factory.js#2532: 332.9533918342796 [07:47:37] [INFO] mystery_lands_mine_factory.js#2532: 500.0 [07:47:37] [INFO] mystery_lands_mine_factory.js#2532: 0.0 [07:47:37] [INFO] mystery_lands_mine_factory.js#2532: 0.0

#

So it seams to work

#

Thanks a lot