Using a float as a dictionary Key

Christopher Correll's icon

Hello I am trying to retrieve a value from a dict using a float as a key. I keep getting the error "Index out of range for key." When I try using an int as a key everything works fine. Here is an attached example reproducing the error:

Max Patch
Copy patch and select New From Clipboard in Max.

I am new to max any help will greatly be appreciated!

Asher's icon

Hey,

been working a lot with dictionaries recently, Keys are always (?) stored as symbols, so the message needs to be:

Get "1.1" - as well.

Formatting this is outside of a message box a bit tricky because of the way Max handles floating points and symbols. Here's a kind of hacky solution: you need to extract the integer and decimal parts of the input float and reformat them as a string using the symout property of sprintf. There's probably other solutions but here's mine (lol)

Max Patch
Copy patch and select New From Clipboard in Max.

Christopher Correll's icon

I was trying to hack something together with sprintf as well but couldn't get it working as I wasn't extracting the floating point into integer representations like your work around.

Does this need to be done due to floating points be treated internally in max as something different then what's on the screen ie adding 0's to the float to match resolution?

Thank you so much this worked like a charm!

Asher's icon

yes, its worth printing to the max console when working with floats - the float numberbox does not show the precise value