-5

I have a string something like this

String str = "1432.768641f";

I want to parse it to float

I've done something like this

float xAxis = Float.valueOf(str);

it returns 1432.7687 but my expected ans is 1432.768641

is it possible to get such result without roundup ?. Any help will be appreciated

I need this result for this.

    package com.view9.stoddart.opengl;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.FloatMath;
import android.view.MotionEvent;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

public class StoddartRenderer implements GLSurfaceView.Renderer {
    private Context mContext;
    private DoubleBuffer mVertexBuffer = null;
    private ShortBuffer mTriangleBorderIndicesBuffer = null;
    private int mNumOfTriangleBorderIndices = 0;

    public float mAngleX = 0.0f;
    public float mAngleY = 0.0f;
    public float mAngleZ = 0.0f;
    private float mPreviousX;
    private float mPreviousY;
    private final float TOUCH_SCALE_FACTOR = 0.6f;
    int pointers = 0;
    float distance = 1;
    RenderListener renderListener;

    public void setRenderListener(RenderListener renderListener) {
        this.renderListener = renderListener;
    }


    public StoddartRenderer(Context context) {
        mContext = context;
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glTranslatef(0.0f, 0.0f, -3.0f);
        gl.glRotatef(mAngleX, 1, 0, 0);
        gl.glRotatef(mAngleY, 0, 1, 0);
        gl.glRotatef(mAngleZ, 0, 0, 1);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);

        // Set line color to green
         gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

        // Draw all lines
        gl.glDrawElements(GL10.GL_LINES, mNumOfTriangleBorderIndices,
                GL10.GL_UNSIGNED_SHORT, mTriangleBorderIndicesBuffer);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
        gl.glClearColor(1f, 1f, 1f, 1);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
        gl.glEnable(GL10.GL_DEPTH_TEST);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // Get all the buffers ready
        setAllBuffers();
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (height == 0) height = 1;
        gl.glViewport(0, 0, width, height);
        float aspect = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
//        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glFrustumf(-aspect, aspect, -1.0f, 1.0f, 1.0f, 0.0f);
    }

    private void setAllBuffers() {
        ArrayList<CoOrdinatesModel> cordinateList = new ArrayList<>();

        cordinateList.add(new CoOrdinatesModel(0.0, 0.0, 0.0));
        cordinateList.add(new CoOrdinatesModel(.1432768641, -.2475000000, .2894649969));
        cordinateList.add(new CoOrdinatesModel(.1433674949, -.2474093692, .2895072588));
        cordinateList.add(new CoOrdinatesModel(.1433674949, -.2475000000, .2894649969));
        cordinateList.add(new CoOrdinatesModel(.1433674949, -.1500000000, .3349299939));
        cordinateList.add(new CoOrdinatesModel(.1433674949, -.3450000000, .2440000000));
        cordinateList.add(new CoOrdinatesModel(.0224000000, -.2137500000, 0.000000));
        cordinateList.add(new CoOrdinatesModel(.0225000000, -.2136500000, 0.000000));
        cordinateList.add(new CoOrdinatesModel(.0225000000, -.2137500000, 0.000000));
//        cordinateList.add(new CoOrdinatesModel(.0224000000, -.0862500000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000, -.0861500000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.0862500000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5775000000f, .3451000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5776000000f, .3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5775000000f, .3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.8999000000f, -.3225000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3224000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3225000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.8999000000f, -0.000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .001000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -0.000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0111792893f, .0113207107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0111792893f, .0111792893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0112500000f, .0112500000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.0225000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0113207107f, -.3111792893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0111792893f, -.3111792893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0112500000f, -.3112500000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.2775000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.3525707107f, -.0524292893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.3525707107f, -.0525707107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.3525000000f, -.0525000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4275707107f, .1725707107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4275707107f, .1724292893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4275000000f, .1725000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2362500000f, -.1499000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2363500000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2362500000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7499292893f, .1500707107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7500707107f, .1500707107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7500000000f, .1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7500707107f, -.1499292893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7500707107f, -.1500707107f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.7500000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3000000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4275000000f, -.349000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4276000000f, -.3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4275000000f, -.3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5249292893f, -.0749292893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5250707107f, -.0749292893f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.5250000000f, -.0750000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.6000000000f, -0.000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.4500000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.8999000000f, .3225000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3226000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3225000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3000000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0451000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, -.1499000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, -.3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0524000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.1499000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.1500000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.0525000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.2475000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2549000000f, .1950000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .1951000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .1950000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .3450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.1050000000f, .0451000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.1051000000f, .0450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.1050000000f, .0450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .0450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, .0450000000f, 0.000000f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.2775000000f, 2.754757671f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.0225000000f, 2.754757671f));
//        cordinateList.add(new CoOrdinatesModel(.4500000000f, -.1500000000f, 3.349299939f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.2475000000f, 2.894649969f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.300000000f, 2.649838447f));
//        cordinateList.add(new CoOrdinatesModel(.6000000000f, -0.000000f, 4.048761430f));
//        cordinateList.add(new CoOrdinatesModel(.0525000000f, -.0525000000f, 2.894649969f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, -.3450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3000000000f, 2.649838447f));
//        cordinateList.add(new CoOrdinatesModel(.0225000000f, -.1500000000f, 3.349299939f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .3450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.2550000000f, .0450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(-.0450000000f, .0450000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(0.000000f, -.3000000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, -.3000000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.9000000000f, .3000000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.3000000000f, .3000000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(.3000000000f, 0.000000f, 2.440000000f));
//        cordinateList.add(new CoOrdinatesModel(0.000000f, 0.000000f, 2.440000000f));


        List<Double> vList = new ArrayList<>();
        for (int i = 0; i < cordinateList.size(); i++) {
            vList.add(cordinateList.get(i).getxAxis());
            vList.add(cordinateList.get(i).getyAxis());
            vList.add(cordinateList.get(i).getzAxis());
        }

        Double[] vertexlist = new Double[vList.size()];
        int count = 0;
        for (Double f : vList) {
            vertexlist[count++] = f;
        }

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asDoubleBuffer();
        mVertexBuffer.put(vertexlist);
        mVertexBuffer.position(0);

        // Set triangle border buffer with vertex indices
        short trigborderindexlist[] = {
//                89, 78,
//                79, 89,
//                78, 81,
//                90, 86,
//                83, 82,
//                88, 83,
//                92, 79,
//                87, 78,
//                79, 85,
//                91, 80,
//                90, 84,
//                89, 80,
//                88, 84,
//                83, 84,
//                82, 87,
//                80, 84,
//                86, 88,
//                87, 92,
//                81, 85,
//                91, 90,
//                92, 91,
//                98, 97,
//                93, 98,
//                94, 93,
//                95, 94,
//                96, 95,
//                97, 96

                1,2,2,3,4,5


        };
        mNumOfTriangleBorderIndices = trigborderindexlist.length;
        ByteBuffer tbibb = ByteBuffer.allocateDirect(trigborderindexlist.length * 2);
        tbibb.order(ByteOrder.nativeOrder());
        mTriangleBorderIndicesBuffer = tbibb.asShortBuffer();
        mTriangleBorderIndicesBuffer.put(trigborderindexlist);
        mTriangleBorderIndicesBuffer.position(0);
    }

    public boolean onTouchEvent(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
                mAngleY = (mAngleY + (int) (dx * TOUCH_SCALE_FACTOR) + 360) % 360;
                mAngleX = (mAngleX + (int) (dy * TOUCH_SCALE_FACTOR) + 360) % 360;
                break;

        }
        mPreviousX = x;
        mPreviousY = y;
        return true;
    }
//    public boolean onTouchEvent(MotionEvent e) {
//        switch (e.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                pointers = 1;
//                return true;
//            case MotionEvent.ACTION_POINTER_2_DOWN:
//                pointers = 2;
//                distance = fingerDist(e);
//                return true;
//            case MotionEvent.ACTION_MOVE:
//                if (pointers == 2) {
//                    float newDist = fingerDist(e);
//                    float d = distance / newDist;
//                    renderListener.setZoomValue(d);
//                    //
//                    distance = newDist;
//
//                }
//                return true;
//            default:
//                return false;
//        }
//    }

    protected final float fingerDist(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    interface RenderListener {
        void setZoomValue(float d);
    }

}

I want to set the float value to the CoOrdinatesModel

Ram Mandal
  • 1,899
  • 1
  • 20
  • 35

2 Answers2

0

Try using double instead of float to preserve the decimal precision.

Double xAxis = Double.parseDouble(str);

Read this also, it would be helpful for you to understand

Community
  • 1
  • 1
Dilberted
  • 1,172
  • 10
  • 23
  • I could but unfortunately in open gl ByteBuffer's object.asFloatBuffer() doesnot work with double – Ram Mandal Oct 13 '15 at 05:12
  • not sure what you are trying to achieve (cannot guess without looking at the code) but why don't you use asDoubleBuffer() – Dilberted Oct 13 '15 at 05:21
  • ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer = vbb.asDoubleBuffer(); mVertexBuffer.put(vertexlist);//here it shows error mVertexBuffer.position(0); – Ram Mandal Oct 13 '15 at 05:26
  • mVertexBuffer is still of type FloatBuffer in the code above – Dilberted Oct 13 '15 at 05:34
  • ya I've changed it to DoubleBuffer but still not worked so I've share the previous code. any solution ? I'm to upset :( – Ram Mandal Oct 13 '15 at 05:36
  • if I change to DoubleBuffer then I m getting error in mVertexBuffer.put(vertexlist); it says cannot resolve method put('java.lang.Double[]') – Ram Mandal Oct 13 '15 at 05:37
  • You would have to change the reference from float to double in your code. For eg. Vertexlist is also of type float – Dilberted Oct 13 '15 at 05:45
  • When you change vertexlist from float [] to double[] .. do you still get the same error message ? – Dilberted Oct 13 '15 at 05:47
  • 1
    Use exactly the same double[] vertexlist = new double[vList.size()]; – Dilberted Oct 13 '15 at 05:52
  • voilla !!! thats worked but app crashes and crashes point shows in this place mVertexBuffer.put(vertexlist); BufferOverflowException – Ram Mandal Oct 13 '15 at 05:56
  • The reason why your code is not working is due to fact your are using Double object [] but what doublebuffer expects is primitive type i.e. double [] – Dilberted Oct 13 '15 at 05:57
  • What is the exception in logcat? – Dilberted Oct 13 '15 at 05:58
  • here is logcat exception Process: com.view9.stoddart, PID: 9062 java.nio.BufferOverflowException at java.nio.Buffer.checkPutBounds(Buffer.java:189) at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:320) at java.nio.ByteBufferAsDoubleBuffer.put(ByteBufferAsDoubleBuffer.java:161) at java.nio.DoubleBuffer.put(DoubleBuffer.java:386) at – Ram Mandal Oct 13 '15 at 05:59
  • com.view9.stoddart.opengl.StoddartRenderer.setAllBuffers(StoddartRenderer.java:205) at com.view9.stoddart.opengl.StoddartRenderer.onSurfaceCreated(StoddartRenderer.java:72) at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1509) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248) – Ram Mandal Oct 13 '15 at 05:59
  • 1
    Check the size of vbb.asDoubleBuffer(); and vertexlist. Most likely that would be the cause – Dilberted Oct 13 '15 at 06:02
  • ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 8); that was because of this I set 4 earlier and changed to 8 for double it worked. Thanks bro again u saved my ass – Ram Mandal Oct 13 '15 at 06:05
  • its not drawing any shapes now bro. :( – Ram Mandal Oct 13 '15 at 06:34
0

The problem is that Float has finite precision. You can try using double. Check this:

        String str = "1432.768641f";
        float xAxis = Float.valueOf(str);
        System.out.println(xAxis);

        double xAxisD = Double.valueOf(str);
        System.out.println(xAxisD);

It prints:

1432.7687

1432.768641

Now why so? A float is 4 byte whereas double is 8 bytes. A double has more precision and less rounding errors. The following excerpt is taken from this excellent link:

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • I could but unfortunately in open gl ByteBuffer's object.asFloatBuffer() doesnot work with double – Ram Mandal Oct 13 '15 at 05:15
  • convert string to double first, then again convert to float from double. so that you can eliminate the 'f' which u cant convert from string to float – Praneeth Oct 13 '15 at 05:30