Converting a number to binary and back as a test in Java

Posted on

Problem

This is one of my first takes on Java, coming mainly from Matlab I found I had many things to learn.
The code will try to convert a number into an array of ones and zeros and then do the opposite returning you the original number (or at least something very close to it, as it is hard to return the exact number). It is not tested very well, so I can only tell that it is symmetric and not that it is correct.

It would be nice to know any errors I have made, or things that I could improve. An idea I had was using BigInt, I have read about it but not yet used it.

If you could focus on the language specific issues and kindly point them out I would be grateful.

package baseconversion;

import java.lang.Math;
import java.util.Arrays;

public class FloatToBin{

    public static double pow2(double a, double b) {
        return a * (java.lang.Math.pow(2, b));
    }

    public static double rem(double a, double b) {
        return a % b;
    }

    public static double fix(double val) {
        if (val < 0) {
            return Math.ceil(val);
        }
        return Math.floor(val);
    }

    public static double vConv(int[] a, double[] b) {
        int aRows = a.length;
        int bRows = b.length;
        double c = 0;
        for (int i = 0; i < aRows; i++) { 
                c += a[i] * b[i];
        }
        return c;
    }

    public static void main(String []args){
        double a = 340.3562; 
        int n = 20;   
        int m = 20;

        int count = 0;

        int[] out = new int[n + m];       

        for (int i = -(n-1); i <= m; i++) {
             out[count] = (int)rem(a * pow2(1, (double)i), 2);
             count++;
        } 

        double[] temp = new double[n + m];
        double origVal;
        count--;
        int revCount = 0; 

        for (int ii = n - 1; ii >= -m; ii--) {
             temp[revCount] = pow2(1, (double)ii);
             count--;
             revCount++;
        }

        origVal = vConv(out, temp);      

        System.out.println("Original number is:");
        System.out.println(a);
        System.out.println("Binary representation is:");
        System.out.println(Arrays.toString(out));
        System.out.println("Inverting we get:");
        System.out.println(origVal);
    }
}

Solution

Cryptic names

int n = 10;   
int m = 20;

Please give longer and more descriptive names to n and m

Overworked main

I would define outside of main

public static double FloatToBin(float n) {

}

public static double BinToFloat(bin n) {

}

So that main can contain the IO only.

Use imports consistently. You’ve fully qualified java.lang.Math.pow but imported java.lang.Math and called Math.floor. Do one or the other (the second being better). You can also statically import methods, e.g. import static java.lang.Math.pow and then just call pow(2, b).

The rem function seems a bit pointless to me. I’d just inline it.

The pow2 function is always called with a == 1. Ged rid of the argument. Then get rid of the function and just call pow(2, x) directly.

Leave a Reply

Your email address will not be published. Required fields are marked *