Simple arithmatic error??? what am i doing wrong?

java is not multiplying large numbers correctly??
run the following program:
public class Test {
  public static void main(String[] args) {
    System.out.println("testing arithmatic.....");
    double a = (double) 27051900;
    float  b = (float) 56.26;
    int    c = (int) (((double) a) * ((float) b));
    System.out.println(a + " * " + b + " = " + c);
    System.out.println("trusty calculator says correct answer: _1,521,939,894_");
    System.our.println("java is off by: " + (1521939894 - c));
    double x = (double) 12;
    float  y = (float) 6;
    int z = (int) (((double) x) * ((float) y));
    System.out.println(x + "*" y + " = " + z);
    System.out.println("correct answer: _72_ [correct]");
    System.out.println("___end test___");
}i am mostly new to java.
what could be causing this strange behaviour?

The Java primitive data type float is 4 bytes, or 32 bits:
1 bit Sign 8 bit exponent 23 bit mantissa
So you result have not be more than 8 388 608 when you operate with float.
Result can be more, but in this case you will lost first digits.
So to receive correct result in this case you have use double.
If you change float to double result will be correct.
you can see :
public class test {
public static void main(String[] args) {
System.out.println("testing arithmatic.....");
double a = (double)27051900;
double b = (double)56.26;
double c = (int)(((double) a) * ((double) b));
//(int) (((double) a) * ((float) b));
System.out.println(a);
System.out.println(b);
System.out.println(a*b);
System.out.println(c);
System.out.println(a + " * " + b + " = " + c);
System.out.println("trusty calculator says correct answer: _1,521,939,894_");
System.out.println("java is off by: " + (1521939894 - c));
double x = (double) 12;
float y = (float) 6;
int z = (int) (((double) x) * ((float) y));
System.out.println(x + "*" y " = " + z);
System.out.println("correct answer: _72_ [correct]");
System.out.println("___end test___");
Result:
init:
deps-jar:
Compiling 1 source file to C:\Apache\htdocs\petals\Java\JavaApplication1\build\classes
compile:
run:
testing arithmatic.....
2.70519E7
56.26
1.521939894E9
1.521939894E9
2.70519E7 * 56.26 = 1.521939894E9
trusty calculator says correct answer: _1,521,939,894_
java is off by: 0.0
12.0*6.0 = 72
correct answer: _72_ [correct]
___end test___
BUILD SUCCESSFUL (total time: 0 seconds)

Similar Messages

  • I am trying to make a time lapse video with Premier Element 13. When trying to publish it at about 15% of work done it stops and in a new screen it says an unknown error, what am I doing wrong?

    I am trying to make a time lapse video with Premier Element 13. When trying to publish it at about 15% of work done it stops and in a new screen it says an unknown error, what am I doing wrong?

    Click on the blue Internet Recovery in nbar's post. That is a link to what computers can run Internet Recovery.
    Do a backup,  preferable 2 separate ones on 2 drives. Boot to the Recovery Volume (command - R on a restart or hold down the option/alt key during a restart and select Recovery Volume). Run Disk Utility Verify/Repair and Repair Permissions until you get no errors.  Reformat the drive using Disk Utility/Erase Mac OS Extended (Journaled), then click the Option button and select GUID. Then re-install the OS.
    OS X Recovery
    OS X Recovery (2)
    When you reboot, use Setup Assistant to restore your data.

  • I am new to iPad.  I am trying to download a movie and it tells me "download error" what am i doing wrong?

    I am trying to download a movie from iTunes and it gives me an error message "download error - retry" what am I doing wrong??

    I must found this tip when I did a Google search. The text inside of the quotation marks is from this site.
    http://www.eddieoneverything.com/articles/how-to-fix-the-ipodiphone-download-err or-tap-to-retry-error-without-using-your-pc.php
    "I had this problem as well, however I did find one way to get this to download on my iPhone. What I found that worked was to tap to retry the download and as it’s attempting to download it again I hit the pause button (I had to be quick). Once it was paused I hit the button again to resume the download and it successfully completed. I hope this helps some of you out until Apple fix this bug."
    This was posted in an iPhone forum, but it applies to the iPad as well. Maybe this will work for you.

  • ITunes Library Error - what am I doing wrong??

    I just bought a new 80 GB video iPod and wanted to import my existing library (I was previously using a nano). When I uploaded the info for my new iPod I got errors from Windows opening iTunes and it kept shutting down my internet connection saying that Windows was experiencing errors - would I like to report.
    I completely uninstalled and reinstalled iTunes. The install was sucessful, however when I try to open iTunes for the first time to access my library, I get the following message:
    The file "i Tunes Library.itl" cannot be read because it was created by a newer version of iTunes.
    The only option it gives me is to hit the "OK" button and exit out of the program. I've uninstalled and reinstalled, restarted and read the FAQ's on iTunes, scanned my PC for viruses and uninstalled everything that might be conflicting - nothing has allowed me to get past this error.
    What are my next steps?
    Thanks in advance - P. Pittman
    HP Pavilion zv6000   Windows XP  

    I just bought a new 80 GB video iPod and wanted to import my existing library (I was previously using a nano).
    Are you importing the "iTUnes Library/xml" file from one PC to another, or do you mean your trying to import your library/songs from a nano?

  • Calling Matlab from Labview returns a 1047 error - what am I doing wrong

    above

    Does your script work fine in MATLAB? Do you run a VI or an .exe?
    MATLAB must be running, otherwise LabVIEW will be unable to access the MATLAB server. This will cause the 1047 error. Manually start MATLAB, and your program should run without the error.
    The following KnowledgeBase may also be useful in troubleshooting this issue
    LabVIEW 6.x and Earlier Does Not Interface Properly with MATLAB 6.1 or MATLAB 6.5
    Zvezdana S.
    National Instruments

  • I am trying to restore my new ipod touch and it is coming up as error, what am i doing wrong?

    I am trying to restore my sons new ipod touch i am trying to restore it but it keeps coming up as error 1611, i have removed all security software and tried again but it has made no difference, any advice?

    Error 1611: This error may indicate a hardware issue with your device. Follow the steps in this article. Alsoattempt to restore while connected with a known-good 30-pin Dock Connector cable, computer, and network to isolate this issue to the device. The MAC address being missing or the IMEI being the default value (00 499901 064000 0) can also confirm a hardware issue. Out-of-date or incorrectly configured proxy or security software, such as FoxyProxy, can cause error 1611. To troubleshoot third-party security software, follow these steps.
    Above from:
    http://support.apple.com/kb/TS3694
    As the article says, you may have a hardware problem.

  • I'm burning dvd's that won't play on a regular dvd player.  What am I doing wrong?  Are there some simple things to check to figure this out?  (Sorry, I know this has been asked many times)

    I'm burning dvd's that won't play on a regular dvd player.  What am I doing wrong?  They play fine on computers but not on dvd players.  Are there some simple things to check to figure this out?  (Sorry, I know this has been asked many times).  Created in DVD studio pro.

    Hi
    Just my thoughts to add to the excellent answers above.
    • BRAND of DVD used - Matters when doing Video-DVDs
    Memorex, NoName etc cheap brands usually do NOT work
    I only Use Verbatim (more organic dye in these)
    • Type of DVD - matters
    - DVD+/RW - usually results in problems
    - DL-DVDs are more problematic than SL-DVDs
    - DVD+R are a more modern sort - BUT PLAYS BADLY or not at all on older DVD-players
    I only use DVD-R
    • BURN SPEED - matters
    - Best in iDVD app - IS NOT BEST at all but results in lot's of Burn Errors = Doesn't play well or at all
    I set this down to x2 or x4 max.
    Yours Bengt W

  • General error:out of memory. what am i doing wrong?

    My free agent 721.5 GB  free space, I have set my system settings all the the free agent memory. I have been working on a project with 3 sequences in them,JPEG, clips Mp4 . and had no problem playing them, editing  them. Until now....  .. my minimum auto allowable free space on scratch disk is set to default: 2047 MB when I try to open my sequence says : general error.... I click ok, then another window opens saying, out of memory. my project opens, but only one sequence opens. the others have been edited more. plus The main video will not open. what am I doing wrong?

    productivity wrote:
    So I have been successfully able to edit my jpeg and .Mp4 in FCP. Its the mpeg-2 that dont work on FCP. MP4-quick time player plays my files. 
    QuickTime player plays your mp4 files. Because it's a playback codec (and more recently, an aquisition codec). It is not an editing codec. Why? Like MPEG2 it uses a Long GOP format.
    What's a Long GOP codec? Simple explanation. It is a method of storing lots of video in a small space. It does this by throwing away "redundant" information. Frame 1 is a complete image, as is frame 15, 30, 45 etc. those in between only contain information on what's different or predict what it might be (Oooh artifacting). Think of someone standing in front of a plain colored wall, waving their arm. The difference is the arm movement. Everything else is more or less the same as in frame 1.
    But you want to make a cut on frame 8. Wait. Frame 8 can't exist without information from frame 1. Frame 8 needs to be independent so you can make that cut. That can only happen if you transcode the footage so that every frame is independent. Because you want to cut right there and not kind of there. Of course, trancoding to an editable format turns the couple of gigs of footage you shot into considerably more.
    So far, you have been lucky. You got away with editing a few simple cuts and probably had to render. But, if you choose your editing codec wisely and set FCP up properly, you won't have to render at all. When your editing gets more complex, using layers and compositing, grit your teeth and wait or ask whatever god you pray to for mercy.
    Search terms for this forum: MPEG 4 (or MP4) is not an editing codec. H.264 is not an editing codec. Why don't my mp3 audio files work in FCP?
    Things to Google: Long GOP format. (If you want the techie explanation.)

  • I keep getting this error in Dreamweaver when I am trying to upload my website?  Can you tell me what I am doing wrong?  here is the error message: /html - error occurred - Unable to create remote folder /html.  Access denied.  The file may not exist, or

    I keep getting this error in Dreamweaver when I am trying to upload my website?  Can you tell me what I am doing wrong?  here is the error message: /html - error occurred - Unable to create remote folder /html.  Access denied.  The file may not exist, or there could be a permission problem.   Make sure you have proper authorization on the server and the server is properly configured.  File activity incomplete. 1 file(s) or folder(s) were not completed.  Files with errors: 1 /html

    Nobody can tell you anything without knowing exact site and server specs, but I would suspect that naming the folder "html" wasn't the brightest of ideas, since that's usually a default (invisible) folder name existing somewhere on the server and the user not having privileges to overwrite it.
    Mylenium

  • I downloaded an album from iTunes and some of the songs downloaded but others didn't there was an error message 8003 Unknown error occurred. While I downloaded the album to my phone, it wouldn't download to my mac, what am I doing wrong???

    I purchased an album on ITunes store, while the items downloaded to my phone, it didn't download to my mac. Some of the songs downloaded but not the majority, when I try to download the songs, it gives me an error 8003 Unknown error occured. What am I doing wrong? If I can't download this album, how do I get a credit??

    Hello mercdw,
    Once the music has been downloaded to your iPhone, you can transfer purchases from your device to iTunes.
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer
    http://support.apple.com/kb/HT1848
    Alternatively, the following article provides further information regarding this error and steps that can help resolve the issue.
    "Error -50," "-5000," "8003," "8008," or "-42023"
    These alerts occur due to timeouts or conflicts trying to write a file during download.
    If you encounter this issue while accessing iTunes Store:
    See iTunes 9: "One Moment Please" or "Error (-50)" message when accessing iTunes Store
    If you encounter this issue while while downloading something from the iTunes Store:
    Delete your iTunes Downloads folder, located in:
    Mac OS X:
  ~/Music/iTunes/iTunes Media/Downloads   Note: "iTunes Media" may appear as "iTunes Music. Also, the tilde (~) refers to your Home directory.
    Windows Vista:
  \Users\username\Music\iTunes\iTunes Media\Downloads\
    Windows 7:
  \Users\username\My Music\iTunes\iTunes Media\Downloads\
    Windows XP:
  \Documents and Settings\username\My Documents\My Music\iTunes\iTunes Media\Downloads\
    After locating your iTunes Downloads folder:
    Quit iTunes.
    Delete the Downloads folder on your computer.
    Open iTunes.
    Choose Store > Check for Available Downloads.
    Enter your account name and password.
    If you encounter this issue while while downloading Digital Copies using Windows:
    Use MSCONFIG (directions for Windows XP and Windows Vista/Windows 7) to disable conflicting software.
    If using MSCONFIG steps resolves the issue, you may want to use the System Configuration Utility to turn on the third-party System Services and Startup Items one at a time (restarting your computer after turning on the item or items) to identify which System Service or Startup Item is causing the conflict.
    You can turn all of them back on by selecting the Normal Startup option under the General tab of the System Configuration Utility window, but please note that this may cause the issue to reoccur.
    If you are able to isolate the issue to a particular third-party software, you may wish to contact them to let them know of the conflict.
    iTunes: Advanced iTunes Store troubleshooting
    http://support.apple.com/kb/TS3297
    Cheers,
    Allen

  • I am trying to burn a cd from my itunes playlist but it keeps canceling my burn and says their is an error 4450. What am I doing wrong?

    I am trying to burn a cd from my itunes playlist but it keeps canceling my burn and says their is an error 4450. What am I doing wrong?

    Could you post your diagnostics for us please?
    In iTunes, go "Help > Run Diagnostics". Uncheck the boxes other than DVD/CD tests, as per the following screenshot:
    ... and click "Next".
    When you get through to the final screen:
    ... click the "Copy to Clipboard" button and paste the diagnostics into a reply here.

  • My Top Rated songs, when I synch my iP4S - comes up with an error message saying it can't find all the songs. When I check, library has songs listed but with a "!" bubble next to them. When I click on them they play. What am I doing wrong?

    When synching, itcomes up with an error message saying it can't find all the songs. When I check, library has songs listed but with a "!" bubble next to them. When I click on them they play. What am I doing wrong? This happens every time so I click them and it sorts them (boring with 200 odd). How can I prevent this?

    Once you successfully add you iTunes library to iTunes Match, you go to Settings>iTunes & App Store on your iOS device and turn on iTunes Match.  Your iTunes Match library will then appear on your iOS device.

  • Im trying to convert a PDF into an excel document and I keep getting an error message that reads "An error has occurred while trying to access the service". WHat am I doing wrong?

    Im trying to convert a PDF into an excel document and I keep getting an error message that reads "An error has occurred while trying to access the service". WHat am I doing wrong?

    it seems my subscription had expired so I signed up again.. It was still having trouble so I repeated the sign up process again.. Then it worked perfectly.. Unfortunately, I think I just subscribed twice and need to cancel one of them. Ugh. Such a pain when I'm trying to get this project completed. I'll be canceling at least one of the subscriptions in the morning. Adobe is not my favorite company right now. None of this was intuitive. And trying to get help was an absolute waste of an hour.
    Regards,
    Nathaniel
    [removed by moderator]

  • After attempting to process my movie, I get the following message. "The project could not be prepared for publishing because an error occurred. (OpWrErr: file already open with with write permission). Any ideas what I'm doing wrong?

    After attempting to process my movie, I get the following message. "The project could not be prepared for publishing because an error occurred. (OpWrErr: file already open with with write permission). Any ideas what I'm doing wrong?

    Hi
    Error -49 opWrErr  File already open with write permission
    Trash the preference files while application is NOT Running.
    from Karsten Schlüter
    Some users notice on exporting larger projects from within iMovie that this operation is aborted with an 'error -49'
    This issue occours only on MacOs machines using 10.7x
    try switching-off the Local Mobile Backup
    in Terminal copy/paste
    sudo tmutil disablelocal
    Re-launch Mac
    Yours Bengt W

  • I am try to burn a dvd from iPhoto in iDVD, when I click burn I get error message I don't have SuperDrive and dvd is ejected. I have burned dvd's before. What am I doing wrong?

    I am try to burn a iPhoto slideshow on to a dvd in iDVD, when I click burn I get error message and dvd is ejected. I have burned dvd's before. What am I doing wrong?

    error message I don't have SuperDrive and dvd is ejected
    If you don't have a SuperDrive you will not be able to burn DVDs. 
    Did iDVD come preinstalled on your Mac?  If it did then you should have a SuperDrive and be able to burn DVDS. Check the Disk Burning section of System Information
    to see what you can read and write with your optical drive:
    OT

  • I'm trying to install the new version, but each time during the installation, "Error writing to file: C:/Program Files (x86)/Common Files/Apple/Mobile Device/Support/AppleMobileDeviceHelper.resources/Info.plist." Keeps popping up. What am I doing wrong?

    I'm trying to install the new version, but each time during the installation, "Error writing to file: C:/Program Files (x86)/Common Files/Apple/Mobile Device/Support/AppleMobileDeviceHelper.resources/Info.plist." Keeps popping up. What am I doing wrong?

    That one's consistent with disk/file damage. The first thing I'd try with that is running a disk check (chkdsk) over your C drive.
    Windows 7 instructions in the following document: How to use CHKDSK (Check Disk)
    Select both Automatically fix file system errors and Scan for and attempt recovery of bad sectors, or use chkdsk /r (depending on which way you decide to go about doing this). You'll almost certainly have to schedule the chkdsk to run on startup. The scan should take quite a while ... if it quits after a few minutes or seconds, something's interfering with the scan.
    Does the chkdsk find/repair any damage? If so, can you get an install to go through properly afterwards?

Maybe you are looking for

  • Preview Page Border

    Hello, I need a little help with my .RTF template to populate page border. This topic is limited but I researched Thread: How to display 'page border' in PDF output file - RTF file contains border - which directs you to another Thread. I was not succ

  • Adobe Muse

    I have Windows 8.1 and tried to update Muse using my CC account, during this process the programme completely disappeared from my system. In the end it failed stating: Exit Code: 7 Please see specific errors below for troubleshooting. For example,  E

  • Oracle Installation on Centos

    I am having problem installing Oracle 10g or 11g enterprise and/ or standard edition on Centos. I have checked the plugins and the directory and even if there is a listener. I can use the SQL, but when I click on "Go to homepage" it takes me to the C

  • How to identify which controller to extend in a standard page?

    Hi  All, we need to extend the controller  of expense audit page in R12  to default  "original package receipt date" field as sysdate  when the user enters the  expense report number and click on go  button. I am new to OAF. we have many controllers

  • Protecting content / locking master Keynote file

    Apple, it's been over 7 years now the iWork community has been screaming for a fix to the most glaring problem with Keynote... being able to protect the content or password protecting the file from being modified. There are so many ways to address th