I am not able to store and retrieve some values of Float types from smart contract. I am about to store the values of IP Address, Geo-location, Mac address etc.. which all are of float types. I know float types are not currently supported in solidity, is there any work around for the same !

up vote 0 down vote accepted

As others mentioned, a Float is not possible in solidity, or more precise, the EVM.

The obvious reason is that floats are, by nature, unpredictable, which would make the outcome of operations different over nodes. Which would lead to forks.

However, all examples you state have nothing to do with Floats:

  • IP-addresses are 32 (IPV4) or 128 (IPV6) bit numbers. They are just formatted weirdly. 127.0.0.1 is just 2130706433
  • Geo- Locations, or pairs of LAT/LON, are decimals, really. Sometimes presented in base60 or base 12 (10"23'0 etc). LAT and LON may look like floats, but geo-locations have a precision built in by their standards: the more decimails you give, the more precise the point is. Depending on your implementation, you might need this precision up to centimeters or kilometers.
  • MAC-addresses are 48-bit numbers, similar to how IP-addresses work. They are, however, often presented in hexadecimal formats.

This is an often-made mistake. We hardly ever need to store floats. We hardly ever need to pass floats along. But we might have floats in a program when operating internally, but even this is not often necessary. In other words: when you think you need to deal with floats: think again: you quite probably don't want floats.

Yes, float is currently not supported in solidity. But you can decimals for that.

And honestly for storing IP address, geolaoctaion in conventional techs like Java, Node, float doesn't seem to a good choice. Float , int is suited when you need to perform some mathematical operations. Else you should store them as string.

You can choose String or bytes32 depending on your choice. See User string or bytes 32?

  • I tried to store with String or bytes32 but the errr is : _Uncaught TypeError: Cannot read property 'length' of undefined at Object.ip.toString _ – Aiswarya Jul 9 at 6:23
  • Where do you exactly get this error? Need to see the cod, hard to tell the issue without that. Be sure to use quoutes while dealing with strings. – Prashant Prabhakar Singh Jul 9 at 7:18

you could multiply your float numbers by a factor that always converts them to integers. e.g if your float only has 2 decimal places, factor could be 100. This factor should be applied in your fronted before sending it to your smart contract. So if your user enters 14.32, you pass 1432 to your contract and store it, so when invoking this value from your contract it should be converted before displaying it or using it.

This factor should also be stored on your contract to avoid mistakes.

  • But here for example: IP Address is 117.216.27.150 for that how can i use the conversion as said ? @Roberto C. – Aiswarya Jul 9 at 6:21
  • For IP addresses I believe storing as bytes or string makes more sense, depending if you want to pass them between contracts. – Roberto C. Jul 9 at 6:27
  • For IP address, maybe a uint8[4] would do the trick? It might be a little cheaper to store than bytes. – AnAllergyToAnalogy Sep 14 at 13:41

Not all the data you cited are floating point data: IP and MAC ADDRESS are’t by definition, neither they are geolocation data if you fix the precision. One example of floating number is 3.25E+10, one example of fixed precision data is 12.34, IP number are four bytes long like 127.0.01 and the dot is simply a trick to read them easy, Macaddress is a truly address codified.

Anyway what you need is to represent your number as integer number, as you probably saw in tokens representation.

10.22000001 can easily represented as 1022000001 and so on

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.