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 !
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?
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.
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
I assume that by float types you mean types that are able to store fraction numbers, such as 3.14. Floating point is quite common representation for such numbers, but for Ethereum/Solidity fixed point is considered more promising. Fixed point number of basically a simple fraction with fixed denominator, i.e. fraction
N is a constant and
x may vary.
Currently most common value for
10^18, so fraction numbers are represented as integers, like this:
1.0 -> 1000000000000000000 0.5 -> 500000000000000000 3.14 -> 3140000000000000000
and so on. While Solidity still lacks native support for such numbers, libraries exists for them, such as DS Math.
Though, such representation is convenient for humans who are familiar with decimal numbers, but not for computers that use binary numbers.
You should probably take as look at ADBK Math 64.64 library. It operates with binary fixed point number having 64 binary digits after dot, i.e. denominator is 2^64 rather than 10^18.