testFloatConvert method Null safety

Future<void> testFloatConvert()

Implementation

Future<void> testFloatConvert() async {
  await test('Convert from int to float', () async {
    final Model model = newModel();
    model.wordSize = 32;
    model.yI = BigInt.from(0x25e47);
    model.xI = BigInt.zero;
    model.displayMode = DisplayMode.float(2);
    await expect(model.xF, 155207.0);

    model.displayMode = DisplayMode.hex;
    model.integerSignMode = SignMode.unsigned;
    model.yI = BigInt.one;
    model.xI = BigInt.zero;
    model.displayMode = DisplayMode.float(4);
    model.displayMode = DisplayMode.hex;
    await expect(model.xI, BigInt.parse('ffffffffffffe1', radix: 16));
    await expect(model.yI, BigInt.parse('80000000', radix: 16));
  });
  await test('int DisplayMode mode convert from float', () async {
    await _testConvertFromFloat(0, BigInt.zero, 0);
    await _testConvertFromFloat(
        512, BigInt.one << 31, -22); // 512 = 2<<31 * 2^-22
    await _testConvertFromFloat(
        513, BigInt.one << 31 | BigInt.one << 22, -22); // 512 = 2<<31 * 2^-22

    await _testConvertFromFloat(5e-62, BigInt.parse('2760698539'), -235);
    await _testConvertFromFloat(5e-52, BigInt.parse('3213876089'), -202);
    await _testConvertFromFloat(1.284e-17, BigInt.parse('3973787526'), -88);
  });
  await test(
      'DisplayMode from float to int and back at power of two boundaries',
      () async {
    /// go from a little over 1e-99 to a little under 9.999999999e99,
    /// concentrating on the areas around powers of two.  This is meant to
    /// tease out any rounding errors, especially around the log()
    /// calculations in _IntegerMode.convertValuesFromFloat
    Model model = newModel();
    model.displayMode = DisplayMode.float(9);
    await _testFloatConvertAndBack(model, 0.0);
    await _testFloatConvertAndBack(model, 1);
    await _testFloatConvertAndBack(model, 123);
    await _testFloatConvertAndBack(model, 5.678e99);
    await _testFloatConvertAndBack(model, 5.678e-99);
    for (int exp = -328; exp <= 332; exp++) {
      final double base = pow(2.0, exp).toDouble();
      for (double delta = -pow(10.0, -8.0).toDouble();
          delta <= pow(10.0, -8);
          delta += pow(10.0, -10) * 3) {
        await _testFloatConvertAndBack(model, base + delta * base);
      }
    }
  });

  await test('DisplayMode from float to int and back with random values',
      () async {
    Model model = newModel();
    model.displayMode = DisplayMode.float(9);
    await _testFloatConvertAndBack(model, 1.0625892214194362e+58);
    final Random r = Random();
    const limit = kIsWeb ? 100 : 2000;
    for (int i = 0; i < limit; i++) {
      if (i > 0 && i % 2000 == 0) {
        final percent = (i * 100 / limit).toStringAsFixed(0);
        debugPrint('Random float count $i of limit - $percent%');
      }
      final double m = 22 * r.nextDouble() - 11;
      final int e = r.nextInt(250) - 125; // Generate some out of range
      final double dv = m * pow(10.0, e);
      await _testFloatConvertAndBack(model, dv);
    }
  });
}