Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

cast double array to float

Thread view: 
Philipp - 29 Nov 2006 13:42 GMT
Hello
Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?

Thanks for your answers.
Phil

I now have this (doublePic is of type double[][])

      float[][] floatPic = new float[height][width];
      for(int j=0; j<height; j++){
          for(int i=0; i< width; i++){
              floatPic[j][i] = (float)doublePic[j][i];
          }
      }
Robert Klemme - 29 Nov 2006 14:07 GMT
> Hello
> Is there a nicer (or faster) way to cast a double[][] to a float[][]
[quoted text clipped - 11 lines]
>           }
>       }

This is not a cast but a conversion.  The only thing that could be
improved is to use ".length" in your expressions instead of variables
"height" and "width".

Kind regards

    robert
Chris Uppal - 29 Nov 2006 14:11 GMT
> Is there a nicer (or faster) way to cast a double[][] to a float[][]
> than doing loops?

No.

You may be able to find a pre-packed routing to do it for you (I don't /think/
any exist, but I could easily be wrong); but it'll just be doing loops inside,
the same as if you did it yourself.

   -- chris
Philipp - 29 Nov 2006 18:42 GMT
>>Is there a nicer (or faster) way to cast a double[][] to a float[][]
>>than doing loops?
[quoted text clipped - 4 lines]
> any exist, but I could easily be wrong); but it'll just be doing loops inside,
> the same as if you did it yourself.

I was thinking of the likes as System.arraycopy()  which is a native
method (as far as I can tell) so is maybe faster (?)...

But it needs to convert from double to float while copying...

Phil
Daniel Pitts - 29 Nov 2006 21:00 GMT
> >>Is there a nicer (or faster) way to cast a double[][] to a float[][]
> >>than doing loops?
[quoted text clipped - 11 lines]
>
> Phil

Try arraycopy out, I don't know if it'll work, but it might convert for
you.
Patricia Shanahan - 29 Nov 2006 21:08 GMT
>>>> Is there a nicer (or faster) way to cast a double[][] to a float[][]
>>>> than doing loops?
[quoted text clipped - 13 lines]
> Try arraycopy out, I don't know if it'll work, but it might convert for
> you.

One of its conditions for throwing an ArrayStoreException is "The src
argument and dest argument refer to arrays whose component types are
different primitive types."

Patricia
Chris Uppal - 30 Nov 2006 14:37 GMT
> I was thinking of the likes as System.arraycopy()  which is a native
> method (as far as I can tell) so is maybe faster (?)...

<reminisce>I think it was when 1.3 came out, with the first release of the
Hotspot JIT, that the rumour went around that System.arraycopy() was now
implemented in Java, since that was as fast as a native version would have
been.  I never did bother to check (I dunno why).</reminisce>

Whatever, I don't think that's true anymore.  The 1.5 Sun JMV generates machine
code implementations for the various combinations of types and overlapping vs.
non-overlapping copies as part of its startup sequence, and (I presume) patches
them directly into the implementations of System.arraycopy().  Hard to imagine
Sun's engineers bothering with that unless they knew they could squeeze useful
extra speed that way (I must measure it someday...).

> But it needs to convert from double to float while copying...

Yeah.  I suspect that's the problem.  If there's no hyper-efficient, low-level,
bit-for-bit copy instruction(s) available, then there's not a lot to gain by
having a pre-packaged "wrapper" for low-level code.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.