Using NaN in Java

Using NaN in Java

Java has a value NaN. The value has multiple faces and is returned under different circumstances.

Below code samples will help us understand some of them.

public class JavaNaN{public static void main(String args[]){JavaNaN javaNaN = new JavaNaN();javaNaN.proceed();}private void proceed(){double num = -5;System.out.println("Math.sqrt returns NaN when used on a -ve number");System.out.println("Math.sqrt(" + num + "): " + Math.sqrt(num));System.out.println("
The output when casted to int yields zero (0)");System.out.println("(int) Math.sqrt(" + num + "): " + (int) Math.sqrt(num));System.out.println("
The output when casted to float yields NaN");System.out.println("(float) Math.sqrt(" + num + "): " + (float) Math.sqrt(num));}}/*

Expected output:

[[email protected]]# java JavaNaNMath.sqrt returns NaN when used on a -ve numberMath.sqrt(-5.0): NaNThe output when casted to int yields zero (0)(int) Math.sqrt(-5.0): 0The output when casted to float yields NaN(float) Math.sqrt(-5.0): NaN*/
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes