Skip to content

Cannot get float pointer for value stored as 0 #484

Closed

Description

@koemeet

I am using a web service which doesn't return a floating point all the time. For example, 0 is used to represent 0.0. Because this library needs 0.0 in order to parse it as a float I am having some issues with it. Right now I am unable to get a proper float pointer, simply because the number_float storage is not set.

How would I approach this, since the webservice cannot be changed in order to output 0 as 0.0. I am using lots data that has this, so it would be nice if there was a good solution for this issue.

Any ideas? 😄

Maybe it is a good idea to always store ints and float along side each other?

Activity

  1. changed the title [-]Cannot get float pointer for int storage type[/-] [+]Cannot get float pointer for value stored as `0`[/+] on Mar 6, 2017
  2. koemeet commented on Mar 7, 2017

    @koemeet
    ContributorAuthor

    I am using the following as a temporary solution:

    /// get a pointer to the value (floating-point number)
    number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
    {
    	if (is_number_integer())
    	{
    		m_type = value_t::number_float;
    		m_value.number_float = static_cast<float>(m_value.number_integer);
    	}
    
        return is_number_float() ? &m_value.number_float : nullptr;
    }
  3. nlohmann commented on Mar 7, 2017

    @nlohmann
    Owner

    I understand your problem, but don't know a solution either. Your fix is pragmatic, but I think it's counter-intuitive to change a type with a getter. Maybe someone else has a better idea?

  4. gregmarr commented on Mar 7, 2017

    @gregmarr
    Contributor

    I'd say this would be a reasonable thing for the library client to do, but not for the library itself to do. If the client knows that it needs a float there, then it's perfectly reasonable for the client to make it a float.

  5. nlohmann commented on Mar 11, 2017

    @nlohmann
    Owner

    I think changing the library in this regard is not helpful.

  6. added
    solution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)
    and removed on Mar 11, 2017
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

    kind: questionsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions