Как к обходному решению непоследовательное определение numeric_limits <T>:: минута ()?

Чтобы получить значения ваших свойств Name и Weight, используйте следующий код:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("UToouch");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("Name").getValue(String.class);
        String weight = dataSnapshot.child("Weight").getValue(String.class);
        Log.d(TAG, name + " / " + weight);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);

Выходные данные в вашем logcat будут такими:

rittesh / pv
12
задан 1800 INFORMATION 29 April 2009 в 08:49
поделиться

6 ответов

The behaviour of min() isn't all that strange, it returns FLT_MIN, DBL_MIN or INT_MIN (or their respective values), depending on the type you specialize with. So your question should be why FLT_MIN and DBL_MIN are defined differently from INT_MIN.

Unfortunately, I don't know the answer to that latter question.

My suspicion is that it was defined that way for practical purposes. For integer numbers, you're usually concerned with overflow/underflow, where the minimum and maximum value become of interest.

For floating point numbers, there exists a different kind of underflow in that a calculation could result in a value that's larger than zero, but smaller than the smallest representable decimal for that floating point type. Knowing that smallest representable floating point value allows you to work around the issue. See also the Wikipedia article on subnormal/denormal numbers.

4
ответ дан 2 December 2019 в 06:45
поделиться

I'm not sure of the rationale but it is expected behaviour. Well, in the sense that is how Josuttis (and, presumably the standard) describes it!

min(): "Miniumum finite value (minimum normalized value for floating-point types with denormalization)."

As best I can tell if the type is not an integer (numeric_limits<>::is_integer) and has denormalization (numeric_limits<>::has_denorm) min() will return the smallest representable value by that type. Otherwise it will return the smallest value - which may be negative.

For a more consistent interface check out the Boost numeric/conversion library. Specifically the bounds traits class. Here's a snippet:

cout << "lowest float:" << boost::numeric::bounds<float>::lowest();
cout << "lowest int:  " << boost::numeric::bounds<int>::lowest();

You may also find the boost::integer library useful. It brings some of C99's integer support (like int_least16_t) to C++ and can help select the best sized type for you particular need. An example:

boost::uint_t<20>::fast fastest20bits; // fastest unsigned integer that 
                                       // can hold at least 20 bits.
boost::int_max_value_t<100000>::least  // smallest integer that can store
                                       // the value 100000.

I often find that when I need one of boost::numeric/conversion or boost::integer I need them both.

1
ответ дан 2 December 2019 в 06:45
поделиться

The definition of the smallest value for an empty vector can be argued. If the vector is empty then there is no smallest element.

Prefer to use std::min_element instead:

int main()
{
    std::vector<int> v;
    std::generate_n(std::back_inserter(v), 1000, std::rand);

    std::vector<int>::iterator it  = std::min_element(v.begin(), v.end());
    if (it == v.end())
    {
        std::cout << "There is no smallest element" << std::endl;
    }
    else
    {
        std::cout << "The smallest element is " << *it << std::endl;
    }
}
1
ответ дан 2 December 2019 в 06:45
поделиться

Обходной путь был бы

double val = -std::numeric_limits<double>::max();

Конечно, это не объясняет странное поведение numerics_limits :: min (), которое может быть результатом того, что существуют разные min / max границы для целых чисел (min = -2 ^ n, max = 2 ^ n-1), но не для двойных.

1
ответ дан 2 December 2019 в 06:45
поделиться

Вы можете использовать библиотеки Boost. Библиотека Numeric Conversions предоставляет класс, называемый границами, который можно использовать последовательно.

См. Документацию здесь .

9
ответ дан 2 December 2019 в 06:45
поделиться

numeric_limits::min returned the lowest negative number, all floating point number types, return the smallest positive number when I tried it with Sun CC & g++.

I guess this is because 'smallest' and 'minimum' mean different things with floating point numbers. It is a bit odd though.

Both Sun CC and g++ produce the same result :

short:min: -32768 max: 32767

int:min: -2147483648 max: 2147483647

unsigned int:min: 0 max: 4294967295

long:min: -2147483648 max: 2147483647

float:min: 1.17549e-38 max: 3.40282e+38

double:min: 2.22507e-308 max: 1.79769e+308

long double:min: 3.3621e-4932 max: 1.18973e+4932

unsigned short:min: 0 max: 65535

unsigned int:min: 0 max: 4294967295

unsigned long:min: 0 max: 429496729

template<typename T>
void showMinMax()
{
    cout << "min: " << numeric_limits<T>::min() << endl;
    cout << "max: " << numeric_limits<T>::max() << endl;
    cout << endl;
}

int main()
{
cout << "short:";
showMinMax<short>()
...etc...etc..
1
ответ дан 2 December 2019 в 06:45
поделиться
Другие вопросы по тегам:

Похожие вопросы: