Contents

Baekjun 1008, The question of dividing A and B

   Sep 18, 2024     2 min read

Baekjun No. 1008, this is an article about A/B problem (with.Java).

I want to solve the coding test problem, find out how to solve it differently from the retrospective of the problem I solved, and get to know.

Let’s get to the problem first.

Problem

After receiving the two integers A and B, write a program that outputs A/B.

Input

A and B are given in the first line. (0 < A, B < 10)

Output

Output A/B on the first line.

If the absolute or relative error between the actual correct answer and the output value is less than 10-9, it is the correct answer.

problem solving

import java.util.Scanner;

class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        double a = scan.nextDouble();
        double b = scan.nextDouble();
        scan.close();
        System.out.print(String.format("%.10f", a / b));
    }
}

Solution Description

I was able to solve the problem and think about the difference between float and double.

Java uses two basic data types, double and float, to represent floating-point numbers.

These two types differ in the range and precision of the values that can be stored, with different advantages and disadvantages.

Differences
Precision and storage range:

float:

  • 32-bit (4 bytes) size
  • having a significant number of approximately 7 digits
  • The range of values that can be expressed is approximately ±1.4E-45 to ±3.4E+38

double:

  • 64 bit (8 bytes) size
  • Have a valid number of approximately 15 digits
  • The range of values that can be expressed is approximately ±4.9E-324 to ±1.8E+308
Precision and calculation:

Double has twice the number of bits as float, which allows more valid numbers to be expressed, resulting in higher precision.

This is advantageous when high precision is required, such as scientific and financial calculations.

Floats use less memory and can be advantageous when some graphics applications or memory usage is important, although the precision is relatively low.

pros and cons

float:

  • Advantage: It is advantageous in environments with large arrays or memory constraints due to low memory usage. It can provide better performance in some graphics programming.
  • Disadvantage: It can be inaccurate when dealing with a large number of valid numbers due to low precision. More errors can occur in calculations.

double:

  • Advantage: The precision is high and can accurately handle many valid numbers. This is the default floating point type, so you don’t have to specify an explicit type.
  • Cons: Memory usage is twice as high as float. Some performance sensitive applications may be slower.

Conclusion

For example, in graphical applications, performance can be optimized using float, but in financial calculations, it is important to maintain high precision using double.

When memory constraints are not large and precision is important: use double

When memory usage should be minimized, and precision is relatively less important: use float

Thank you!