Please, can anyone tell me what are the smallest floating-point numbers in c++ and java?


Please, can anyone tell me what are the smallest floating-point numbers in c++ and java?
You can use std::numeric_limits in C++.

http://en.cppreference.com/w/cpp/types/numeric_limits

std::numeric_limits<double>::min() returns the smallest finite value that double can have.
Thanks, it was helpful, sir. But is there any recommendation for java?
Ema wrote:

Please, can anyone tell me what are the smallest floating-point numbers in c++ and java?
Peter87 wrote:
std::numeric_limits<double>::min() returns the smallest finite value that double can have.
Are you sure it is the smallest value? Is it not the lowest value instead?

http://ideone.com/ZnZjYS (edit: now includes lowest())

-2 is lower than 1, but 1 is smaller than -2.
Last edited on
Now this is a bit confusing. I have to admit that I was thinking about the lowest value and thought std::numeric_limits<>::min() returned the lowest but that doesn't seem to be the case for floating point values. It actually returns the smallest/lowest positive value (greater than zero). For the lowest value use std::numeric_limits<>::lowest() instead.
For java:

Smallest positive value
Double.MIN_VALUE
Float.MIN_VALUE


Smallest negative value
-Double.MAX_VALUE
-Float.MAX_VALUE


http://ideone.com/PidnSV
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>

int main()
{
    using limits = std::numeric_limits<double> ;
    
    std::cout << "minimum positive normalised value: " << limits::min() << '\n' 
              << "lowest normalised value: " << limits::lowest() << '\n' 
              << std::boolalpha << "sub-normal values are supported: " << ( limits::has_denorm == std::denorm_present ) << '\n'
              << "for sub-mormal numbers, loss of precision is denormalization loss (not an inexact result): " << limits::has_denorm_loss << '\n'
              << "smallest positive sub-normal value: " << limits::denorm_min() << '\n' ;
}

minimum positive normalised value: 2.22507e-308
lowest normalised value: -1.79769e+308
sub-normal values are supported: true
for sub-mormal numbers, loss of precision is denormalization loss (not an inexact result): false
smallest positive sub-normal value: 4.94066e-324

http://coliru.stacked-crooked.com/a/9faac3b49b8321ee
FWIW, has_denorm_loss is pretty pointless given that it was droped from IEEE-754 in 2008 (here's the motion that brought it up: http://grouper.ieee.org/groups/754/email/msg02407.html )
Topic archived. No new replies allowed.