Going nuts over modulo of two doubles

consider:
double x=2.0;
double y=0.1;
double z=x%y;
boolean perfectFit = (Math.round(x/y)==x/y);
System.out.println("x="+x+",y="+y+",z="+z+",perfectFit="+perfectFit);
// surprisingly (at least for me),
// while perfectFit is true (as expected), z is not zero at all:
x=2.0,y=0.1,z=0.0999999999999999,perfectFit=true
am I missing something?

What happens is quite normal and you will understand this if you try:
public class Modulo {
    public static void main (String [] args) {
        double x = 2.0;
        double y = 0.00001;
        double z=x%y;
        boolean perfectFit = (Math.round(x/y)==x/y);
        System.out.println("rounded: "+Math.round(x/y));
        System.out.println("not rounded: "+x/y);
        System.out.println("modulo: "+z);
        System.out.println(perfectFit);       
}You see Math.round(double) returns a long and you play with double. That means that you lose a bit of presision here... In the above example this becomes obvious for a big number.
Hope that helped
afotoglidis

Similar Messages

  • Backup going nuts

    I've posted questions about this problem with the backup (under 10.8.3) and watching it discovered that while normally the backup takes 3-5 minutes and occasionally 5-10 minutes for large updates of applications, every few days the backup will go nuts and backup 25-50 GBytes. The console logs will show the normal 2-4 GBytes required for padding for 200-1,000 MBytes of files but then just keep going for an hour or two before stopping for no obvious or apparent reason. I've deleted the .v100 file on the TC, move the disk out and back in the spotlight, done checks of the sparsebundle, etc. and nothing indicates anything is wrong.
    It would be nice if someone would make some suggestions what's left to check to determine why the backup is going nuts every 3-4 days and eating lots of unncessary disk space. The backup got better under 10.8.3 but this is still an issue which needs explanation since the log and the backup don't match.

    Does anyone know how to see what the backup is doing or what it's backing up? For the fourth time in 10 days it wrote the lines for the latest example:
    Found 99 files (2.2 MB) needing backup
    2.88 GB required (including padding), 533 GB avaiilable
    And then it went and ran for two hours to backup up over 50 GBytes of what I don't know.
    This is what it does every time and it's not consistent when it does it except it's every 3-4 days. I realize it's not really important since it deletes space to do a backup when the HD runs out of space, but doing these backups and deleting the space when it's full interfers with the disk usage and network resources with other applications running. It doesn't matter if I turn the backup off because it resumes where it was when I turn it back on.

  • Adding two double numbers is a problem

    Hi friends..
    when we try to add the two double numbers say,
    119.52, 10.00
    here we should get 129.52
    but it is giving as 129.51999999999998
    Here i want to round this to two digits after decimal point
    if i round the above value i will get 129.52 and the problem will be solved.
    But we don't know exactly on what basis we are getting the sum as 129.51999999999998.
    Assume that, tomorrow when we are adding some other numbers we may got like
    129.51444444444448
    when we round this we get 129.51
    but actually we have to get 129.52.
    If anyone know why the system is giving that wrong sum amount, please give solution to avoid this.
    To solve this problem, i tried by converting the double numbers to integers and adding these integers and finally converting the sum into double...like
    if
    d1= 119.52
    d2=10
    int x = (int)(119.52*100.00);
    int y = (int)(10.00*100.00);
    int z = x + y;
    double d = (double)(z)/100;
    This is working for this case...
    But when we are applying this approch to other numbers it is giving problem....
    i.e, if i convert like below
    int x = (int)(72.46*100.00);
    this should give x=7246
    but it is giving x=7245
    What is the solution for this problem...please give immediate reply.....

    Assume that, tomorrow when we are adding some other
    numbers we may got like129.51444444444448
    That isn't going to happen. The numerical computations are deterministic. The problem lies in your understanding of how numbers work.
    This has been repeated before so if you want more detail I suggest you search the forums for discussion on precision and double.
    Computers store numbers in binary. The number you are trying to represent is a decimal number. This produces an error in precision.
    The same problem exists in decimal notation. Decimal notation can not accurately represent one divided by three (in decimal 0.33333333.....).

  • Aperture going nuts

    Booted up Aperture today, and it's as if it were posessed!  Not sure if this is some Apple prank in light of Halloween, or whether the application itself is drunk.  I've encountered two wacky issues:
    1) Aperture seems to have "lost" my top-rated picks from a recent interior photography shoot.  When I click on the file, it has a yellow badget (Image Not Found).  It won't allow me to export it.  The image is neither in my Aperture Trash nor my OSX Trash (neither of which have been empited since the images from this shoot were imported).  All other images from this shoot are fine, only the top-rated picks.  Very strange.  See screenshot below.
    2) Other images from that shoot (these are all RAW files) laod fine, except they exhibit drastic colors everytime I load up Aperture.  In some instances the images turn purple, other times yellow.  I can even export these as JPGs and they look completely wild!  See attached images.  RAW adjustements work fine, but there is no way to get back the original colors.  There seems to be adrastic shift in white balance and tint.
    I've already Repaired my Aperture Database and repaired permissions, all to no avail.  These 6 top-rated images (which the client had requested) have just disappeared.
    Does anyone have ANY thoughts on how something like this can even happen? I am running the latest Aperture on 10.8.5 iMac (Late 2012 model, 3.4Ghz i7 with 32GB RAM).
    Please anyone, help because I am pulling my hair out right now, and there is no phone tech support to call!
    http://www.materialized.com/temp/aperture_wild_2.jpg
    http://www.materialized.com/temp/aperture_wild_1.jpg

    Some further observations:
    Images seem to corrupt (and disappear) when I attempt to export originals.  Prior to that, I can make RAW adjustments and export the image as a version (JPG), but once I elect to export the original, Aperture goes nuts.  In one case the image turns purple/pink, in other instances it just turns plain black.  In another instances still, it shows that the image was not found (if that's true, how was I able to export versions and make RAW adjustments just a second ago?).

  • When I tried to update my iPhone 5s to iOS 8.1, I was forced to choose to restore or back up.  Back up wasn't working so I had no choice but to restore.  I lost pictures over the last two years.  Cloud and iTunes backup are not working for me.  Help!

    How can I retrieve photos when it says my cloud and iTunes backups are empty or not backed up?
    When I tried to update my iPhone 5s to iOS 8.1, I was forced to choose to restore or back up.  Back up wasn't working so I had no choice but to restore.  I lost pictures over the last two years.  When I tried my cloud, there was nothing.  I would get notifications that my cloud was full, but I thought there would at least be some photos in there.  When I tried to restore a back up, it said the last time was two years ago.  I thought when you're on wifi, it automatically backs up.  I've charged my iPhone into my Macbook a bunch of times, so something should have backed up.  Is there any hope for my photos?

    Sorry no backup - no pictures.
    Yes phone will automatically backup to icloud when on wifi and phone is charge, but your phone storage was full so it stop backing up.
    Back up and restore your iPhone, iPad, or iPod touch using iCloud or iTunes - Apple Support
    iCloud: iCloud storage and backup overview
    When your phone was connected to your computer, you should've seen an autoplay which would've allowed you to import your pics to your computer as you would a digital camera Import photos and videos from your iPhone, iPad, or iPod touch to your Mac or Windows PC - Apple Support

  • I was at a friends and on Itunes on his computer.. with my apple ID i bought some songs. How can i get those songs onto my computer without going back over to my friends house?

    I was at a friends and on Itunes on his computer.. with my apple ID i bought some songs. How can i get those songs onto my computer without going back over to my friends house? They said i have to have his computer and mine in order to get it onto my computer... well... and on my itunes ID is says i bought them in a recent purchase.

    Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • The trackpad of my 13" macbook pro (mid 2012) has geen going nuts for about a week now. It randomly clicks and drags the cursor to all places on screen, it randomly clicks, selects and swipes. Have tried spm reset and pressure on whole trackpad. Help?!

    It goes nuts for hours and the next day it acts completely fine. My macbook is impossible to work with when the problem is active; it randomly clicks, drags, selects and even selects words (in a yellow colour) to come up with latin(?!) translations.
    Ive tried the reset (option+cmd+r+p) and it seemed to work at first but now it doesnt seem to have any effect whatsoever.
    Also tried to apply pressure to the entire surface of the trackpad or just the top corners; no effect whatsoever.
    It's been quite an expensive purchase for a young student like me and i've really enioyed it until last week.
    I'd be relieved if someone came up with a solution; especially since its warranty has expired for half a year now.

    Hi there Bram3,
    You may find the troubleshooting steps in the article below helpful.
    Portables and Magic Trackpad: Jumpy or erratic trackpad operation
    http://support.apple.com/kb/ts1449
    -Griff W. 

  • Triggering events for the Lost Triggers!!! Going Nuts....

    Gurus,
    Oracle ver 11g
    I am going nuts and crazy here....
    Can you think of any possible reasons that I have lost all my objects that I created for triggers ( approx 65 objects) and how to get back those objects.
    I don't have the script to re-create all those objects again as I did it manually using the Toad and I am 1005 sure I saw all the objects created and was able to see them in sql*developer tool.
    We had a server crash and we had to reboot the server where this oracle is residing, but other than that I am have no reason how the objects disappeared...
    My belief is create trigger is a DDL command Right? so the objects should be there in the DB.but I am not able to see them any possible reasons>?? please advice when time permits
    Thanks
    Sheik

    Thanks ennisb for your reply.
    Do you have an export handy that you could restore from?
    ** No, I am not sure how I could do this. and whether we are doing it. Basically are you asking whether we are backing up the DB daily ( No I guess we are not backing up).
    Is Flashback logging turned on?
    ** Guess Yes, the Flash back is turned on.
    When was this server crash?
    ** I am sure I saw the objects on 8th Apr and were created on 7th and 8th and the Server crash was Y'day ( 09th).

  • Lately my iphone 5 is going nuts for some reason. siri pops up for no reason

    my phone is going nuts, it seem to activate siri on its on and takes pictures on the home scrren and some time the home button get stuck , please i need your help in solving this issue, my phone is new , it happens sometime when my phone is on a table with nothing touching it

    Sounds like you have a home button fault as both symptoms relate to the home button, e.g. Siri is normally activated by holding the home button. Screenshots are normally taken by pressing the sleep/wake button and the home button together so then you are switching on the phone it probably assumes the home button is also being pressed and takes the screenshot.
    You will need to make a Genius Bar reservation at your local store and take it in. They are able to replace the home button module on the iPhone 5 which is most likely what they will do. Just ensure you have a backup in case they do decide to replace the phone.

  • TS3988 trying to buy more storage on the cloud, response is 'there was a problem - try again later' its been doing this over the last two hours - any ideas what the 'problem' is please (assuming its not me) - thanks

    trying to buy more storage on the cloud, response is 'there was a problem - try again later' its been doing this over the last two hours - any ideas what the 'problem' is please (assuming its not me) - thanks

    Additional info on this:
    I strongly suspect this is time machine-related. Even though time machine has been turned off, this happens at intervals of exactly one hour.
    A single entry appears in the kernel log at the precise moment the (first) popup error box appears. This is:
    "AppleSRP started."
    Anyone have any clue how to go about diagnosing or fixing this? I don't even mind trying something brute force (short of wiping the HD and reinstalling lion, which I really DON'T want to do; I don't mind a non-destructive reinstall).

  • Adobe  Reader has not worked on my Safari for months. I have tried a new download 20  times! Works fine on my Google Chrome. Im running MacOS 10.9.3. Help! GOing nuts!!!!

    It has been months since I the adobe reader has been able to read a pdf link from Safari. I have to use Google CHrome if I want to read a pdf. I am running OS 10.9.3. My Mac is only 2 years old. I am going nuts trying to run the download... i have tried countless times and I still get adobvve reader bocked for this website on everything!
    sue

    Ah, Ok. I didn't know what was triggering the message. That is launching indeed. (I have to ask because many people a week don't know what it means).
    This message is the big problem, because it isn't even getting as far as the license agreement.
    I found a little discussion of this. It seems rare, especially on a Mac, but here is one person's advice. Worth a try:
    The error that you are facing might be due the reason that the user logged in doesn't have permissions to access the following directory: "~/Library/Application Support/Adobe". Can you please try the following steps with the user account in which you are facing the error:
    1. Remove the Adobe Reader from Applications directory by moving it to Trash.
    2. Now delete the following directory: "~/Library/Application Support/Adobe". Please note that this path is for the Library folder present inside the user directory that you are currently logged in.
    3. Restart the machine.
    4. Now re-install Adobe Reader XI.
    Tip: In Finder use GO TO FOLDER from the menu and type in "~/Library/Application Support/Adobe" (without the quotes but with the ~)

  • Can not access email addresses when they are a link. F/F goes nuts and "untitleed " keep flashing across the top of the screen.I have to get out of that page fast or manually shut down thwe computer.

    Can not access email addresses when they are a link. F/F goes nuts and "untitled " keep flashing across the top of the screen.I have to get out of that page fast or manually shut down the computer

    Use the trackpad to scroll, thats what it was designed for. The scroll bars automatically disappear when not being used and will appear if you scroll up or down using the trackpad.
    This is a user-to-user forum and most people will post on here if they have problems. You very rarely get people posting to say there update went smooth. The fact is the vast majority of Mountain Lion users will not be experiencing any major problems with the OS, or maybe with apps which are not compatible, but thats hardly Apple's fault if developers don't update their apps.

  • I bought an app that still goes nuts on advertising.  I deleted it and want my money back.  Can I get it?

    I bought an app that still goes nuts on advertising.  I deleted it and want my money back.  Can I get it?

    Hello,
    Thanks for using Apple Support Communities.
    If you'd like to request a refund for a purchase from the App Store, you can submit your request by following the directions below.
    To report a problem or request assistance with an item purchased from the iTunes Store, App Store, Mac App Store, or iBooks Store:
    Visit reportaproblem.apple.com in your browser.
    When prompted, enter the Apple ID and password used to initiate the purchase of the item, then click Sign In.
    Find the item you're having a problem with, then click Report a Problem next to it.
    From the Choose Problem menu, choose the issue you're having.
    Follow the onscreen instructions. You might be asked to describe the problem in a text field.
    Click Submit, and we'll review your problem.
    Report a problem with an item purchased from the iTunes Store, App Store, Mac App Store, or iBooks Store - Apple Support
    Take care,
    Alex H.

  • How to replace one double quotes with two double quotes in XSLT

    How can I replace one double quote to a two double quote in a string in XSLT
    I am passing the parameter string to XSLT template contains the value as
    <xsl:variable name="Description">Hi! "How are you</xsl:variable>
    <xsl:variable name="VQuotes">""</xsl:variable>
    I nead the output as
    Hi! ""How are you.
    Tried with Translate function, but it did not work out
    <xsl:element name="DESCRIPTION_SHORT">
              <xsl:value-of select="translate($Description,'&quot;', VQuotes)" />
            </xsl:element>But it is giving the same result as Hi! "How are you
    When I tried with
    <xsl:element name="DESCRIPTION_SHORT">
              <xsl:value-of select="translate($Description,'&quot;', 'BB')" />
            </xsl:element>
    It gave the result as
    Hi! BHow are you.
    It is replacing only one character with one. how to make it for two characters.
    Am I doing anything wrong in syntax?
    Please help.
    Regards, Vignesh S

    Hi Vignesh,
    Try this.
    Its a two step process:
    Step1: Add the following template would be "called" to do the replacement as your want:
    <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
    <xsl:when test="contains($text, $replace)">
    <xsl:value-of select="substring-before($text,$replace)" />
    <xsl:value-of select="$by" />
    <xsl:call-template name="string-replace-all">
    <xsl:with-param name="text"
    select="substring-after($text,$replace)" />
    <xsl:with-param name="replace" select="$replace" />
    <xsl:with-param name="by" select="$by" />
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="$text" />
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    Step2: Call the above templeate in the place where you want to call, like this:
    <!--Define the variables-->
    <xsl:variable name="Description">Hi! "How are you</xsl:variable>
    <xsl:variable name="sQuotes">"</xsl:variable>
    <xsl:variable name="VQuotes">""</xsl:variable>
    <!--Following call the template which you have defined in step1-->
    <xsl:element name="DESCRIPTION_SHORT">
    <xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
    <xsl:with-param name="text" select="$Description" />
    <xsl:with-param name="replace" select="$sQuotes" />
    <xsl:with-param name="by" select="$VQuotes" />
    </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$myVar" />
    </xsl:element>
    I have tested this and works. And outputs as the following with two-double quote as you want.
    <DESCRIPTION_SHORT>Hi!
    ""How are you</DESCRIPTION_SHORT>
    If this answers your question please mark it accordingly. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

  • I have probably enabled voice over and have to double apps etc however I cannot swipe page up in setting to access accessibility to turn off voice over. Can any body help?

    I have probably enabled voice over and have to double click icons etc to make them work as well as hearing voice commands. However I cannot swipe the General page upwards  to access accessibility to turn it OFF. Anybody had this problem?

    Easiest way to connect to computer and turn it off in itunes. If you can't find how, let us know.

Maybe you are looking for

  • Hard drive filling up, can I use external hard drive for my library?

    Hello all! My hard drive on my Sony laptop only has 30G harddrive, and as I am downloading my Cd collection and buy it tunes videos etc., into my ipod, my computer is slowing down and overloading because of all the other programs I have in there. So

  • Integration with R/3

    Hi All, I would like to know if anyone has some pointers on integrating SAP Business One and SAP R/3. I would like to know about any utilities, programs, examples that's available. All I want to do is to be able to create a Purchase Order in R/3 and

  • Using more than one expresion for if/else statements

    hi im making a program that requires something to happen if 2 conditions are true. so its kinda like if (a = 5 /* whatever the AND operator is *\ b = 5) i would really apreciate it if someone would show me an example of how to do this thanks

  • Reader 7 still installed

    After reading the threads on the link problems with reader 9.3.4, I am not certain that this is an issue now. However, I would still like to have an answer. I installed 9.3.4 a short time ago. I thought I was using it all this time, but I was apparen

  • Getting "An unknown error occurred (-50)" when trying to download my purchases

    For the past few weeks I've been getting "An unknown error occurred (-50)" when trying to download my purchases. I've tried downloading from past purchases and by clicking on the cloud symbol on the files in iTunes itself. Same every time. I'm using