0

I'm having a problem when I try to add "1.1" to a string that I've already changed to double before. I can't work out what's causing the bug.

If you look at the gif you can see that 2.2 +1.1 = 3.30000000003 and then there are other bugs like this

My code :

class _TestPageState extends State<TestPage> {
  String value = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Test")),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              onTap: () {
                setState(() {
                  value = (double.parse(value.isNotEmpty ? value : "0") - 1.1)
                      .toString();
                });
              },
              child: Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.3),
                      offset: const Offset(0, 2),
                      blurRadius: 2,
                      spreadRadius: 1,
                    ),
                  ],
                ),
                height: 60,
                width: 60,
                child: Center(
                  child: Text(
                    "-",
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: Text(
                value.isNotEmpty ? value : "-",
              ),
            ),
            InkWell(
              onTap: () {
                setState(() {
                  double newValue =
                      double.parse(value.isNotEmpty ? value : "0") + 1.1;
                  print(newValue);
                  value = newValue.toString();
                });
              },
              child: Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.3),
                      offset: const Offset(0, 2),
                      blurRadius: 2,
                      spreadRadius: 1,
                    ),
                  ],
                ),
                height: 60,
                width: 60,
                child: Center(
                  child: Text(
                    "+",
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
} 

It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.

GIF BUG

Fugipe
  • 386
  • 1
  • 8

1 Answers1

2

It is most common "bug" for every programming language, well, I mean, all of computers. You know, computer works with 0,1 binary, and floating point number store with those binaries. This "bug" doesn't relate to any particular programming langue or computer architecture, it's a fundamental concept of how floating-point arithmetic is implemented in computers. To make your result more "accurate", you can use techniques like rounding or formatting the result to a desired number of decimal places.

In IEEE 754 standard, real numbers are stored in a format that includes a sign bit, an exponent, and a significand (also known as a mantissa). However, some decimal numbers cannot be represented exactly in binary, leading to a loss of precision.

Read more about IEEE 754 standard.

Nguyen family
  • 749
  • 2
  • 12
  • I'm going to use the decimal package to solve this problem, thank you. https://pub.dev/packages/decimal – Fugipe May 31 '23 at 10:24