Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString method number does not remove sign when rounding double value

QString method number does not remove sign when rounding double value

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 339 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zonman
    wrote on last edited by
    #1

    When you use QString::number(double n, char format, int precision) with 'f' formats - method needs to round value after precision number. For example:

    double  value = 0.47
    QString valueStr = QString::number(value, 'f', 1);
    // valueStr = 0.45
    

    But when you work with value, that rounded into zero, this method 'forget' about sign. It is important, because we can display this string value, it is not just math variable, where +0 = -0. For example:

    double  value = -0.02
    QString valueStr = QString::number(value, 'f', 1);
    // valueStr = -0.0
    

    Is it bug or I do not understand something?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I guess it's debatable. I can see how it might not be desirable in some cases.
      To me this is a feature. The value is clearly negative, so even though you cut down precision it's nice that this is still reflected. Besides, type double implements IEEE standard which has both positive and negative zeros.

      1 Reply Last reply
      2
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @zonman,

        I believe it's expected behaviour. Maybe have a quick read of https://en.wikipedia.org/wiki/Signed_zero

        The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating-point numbers) requires both +0 and −0.
        ...
        Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0.
        ...
        the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems,[1] in particular when computing with complex elementary functions.[2]

        Pretty interesting topic, I reckon :)

        Cheers.

        1 Reply Last reply
        1
        • Z Offline
          Z Offline
          zonman
          wrote on last edited by
          #4

          Thank you for replying, I will read it

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved