Skip to content

Floating-point to BigInteger conversion saturates at the long bounds #788

Closed

Description

@PHJ2000

Converting a finite floating-point value to BigInteger goes through Number.longValue(). Values outside the long range silently saturate instead of retaining their integer value. For example, 1e20 becomes 9223372036854775807.

Reproduced with 3.2.6 and current master (7f345d2):

import org.modelmapper.ModelMapper;
import java.math.BigInteger;
import java.math.BigDecimal;
public class Reproduce {
 public static void main(String[] args) {
  ModelMapper mapper = new ModelMapper();
  for (Double d : new Double[]{1e20,-1e20,1.5}) {
   System.out.println(d+" direct="+mapper.map(d,BigInteger.class)+" viaDecimal="+mapper.map(d,BigDecimal.class).toBigInteger());
  }
 }
}

Actual output:

1.0E20 direct=9223372036854775807 viaDecimal=100000000000000000000
-1.0E20 direct=-9223372036854775808 viaDecimal=-100000000000000000000
1.5 direct=1 viaDecimal=1

After the proposed fix:

1.0E20 direct=100000000000000000000 viaDecimal=100000000000000000000
-1.0E20 direct=-100000000000000000000 viaDecimal=-100000000000000000000
1.5 direct=1 viaDecimal=1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions