Skip to content

Commit

Permalink
fix(libs): reading large float16 values
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Jul 25, 2023
1 parent dc1dd8b commit 9ca2a05
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions contracts/libs/WitnetBuffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,17 @@ library WitnetBuffer {
}
// Compute `2 ^ exponent · (1 + fraction / 1024)`
if (exponent >= 0) {
result = int32(
result = int32(int(
int(1 << uint256(int256(exponent)))
* 10000
* fraction
) >> 10;
) >> 10);
} else {
result = int32(
result = int32(int(
int(fraction)
* 10000
/ int(1 << uint(int(- exponent)))
) >> 10;
) >> 10);
}
// Make the result negative if the sign bit is not 0
if (sign != 0) {
Expand Down
8 changes: 4 additions & 4 deletions test/TestWitnetCBOR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

import "truffle/Assert.sol";
import "../contracts/libs/Witnet.sol";
import "../contracts/libs/WitnetCBOR.sol";
import "../contracts/libs/WitnetLib.sol";

contract TestWitnetCBOR {

Expand Down Expand Up @@ -123,7 +123,7 @@ contract TestWitnetCBOR {

function testBytes32DecodeValueFrom31bytes() external {
bytes memory encoded = hex"581f01020304050607080910111213141516171819202122232425262728293031";
bytes32 decoded = WitnetLib.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 decoded = Witnet.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 expected = 0x0102030405060708091011121314151617181920212223242526272829303100;
Assert.equal(
decoded,
Expand All @@ -134,7 +134,7 @@ contract TestWitnetCBOR {

function testBytes32DecodeValueFrom32bytes() external {
bytes memory encoded = hex"58200102030405060708091011121314151617181920212223242526272829303132";
bytes32 decoded = WitnetLib.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 decoded = Witnet.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 expected = 0x0102030405060708091011121314151617181920212223242526272829303132;
Assert.equal(
decoded,
Expand All @@ -145,7 +145,7 @@ contract TestWitnetCBOR {

function testBytes32DecodeValueFrom33bytes() external {
bytes memory encoded = hex"5821010203040506070809101112131415161718192021222324252627282930313233";
bytes32 decoded = WitnetLib.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 decoded = Witnet.toBytes32(WitnetCBOR.readBytes(WitnetCBOR.fromBytes(encoded)));
bytes32 expected = 0x0102030405060708091011121314151617181920212223242526272829303132;
Assert.equal(
decoded,
Expand Down

0 comments on commit 9ca2a05

Please sign in to comment.