6 Replies - 229 Views - Last Post: 28 March 2014 - 11:42 PM Rate Topic: -----

#1 Parmatheus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-March 14

Question about a part of a code.

Posted 28 March 2014 - 10:05 PM

I'm a new learner, so wanted to know why min is declared 1000000000.0
if the whole code is needed let me know.
System.out.print("Unsafe banks are : ");
        while(true)
        {
            int minId=-1;
            double min=1000000000.0;
            for(int i=0; i<n; i++)
            {
                if(safe[i]==true && assets[i]<min && assets[i]<limit)
                {
                    min = assets[i];
                    minId = i;
                }
            }
            if(minId==1) break;
            safe[minId] = false;
            System.out.print(minId+" ");
            for(int i=0; i<n; i++)
            {
                if(given[i][minId]>0)
                {
                    assets[i]-=given[i][minId];
                    given[i][minId]=0;
                }
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Question about a part of a code.

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,102
  • Joined: 19-March 11

Re: Question about a part of a code.

Posted 28 March 2014 - 10:28 PM

Pet peeve time: it's not "a code". Code is stuff - it's a mass noun, like "oatmeal". You wouldn't have "an oatmeal" and you don't have "a code". You might have "some code" or "a program" or "a piece of code". I realize that many DIC members are not native English speakers, and you might well be one of them, and I'm not offended by the usage - but all the same, it's like fingernails on a chalkboard.

It's not clear to me why a programmer would represent this value in this way. Sometimes people use doubles to represent large numbers, but that wouldn't make sense here since you could represent this value as a long, or even as an int. I can only assume that they thought that because money is often shown with a decimal point in it, that it should be represented as a floating-point quantity, which it shouldn't be.
Was This Post Helpful? 0
  • +
  • -

#3 Parmatheus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-March 14

Re: Question about a part of a code.

Posted 28 March 2014 - 10:40 PM

View Postjon.kiparsky, on 28 March 2014 - 10:28 PM, said:

Pet peeve time: it's not "a code". Code is stuff - it's a mass noun, like "oatmeal". You wouldn't have "an oatmeal" and you don't have "a code". You might have "some code" or "a program" or "a piece of code". I realize that many DIC members are not native English speakers, and you might well be one of them, and I'm not offended by the usage - but all the same, it's like fingernails on a chalkboard.

It's not clear to me why a programmer would represent this value in this way. Sometimes people use doubles to represent large numbers, but that wouldn't make sense here since you could represent this value as a long, or even as an int. I can only assume that they thought that because money is often shown with a decimal point in it, that it should be represented as a floating-point quantity, which it shouldn't be.

Hmm, I just want to learn, not to criticize my english.
but thanks anyways. (Thumbs down) i already know that.
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor  Icon User is online

  • D.I.C Addict

Reputation: 167
  • View blog
  • Posts: 788
  • Joined: 07-February 14

Re: Question about a part of a code.

Posted 28 March 2014 - 11:23 PM

There is either purpose for it , or it is bad code.

Look at where it first is used:
line 8: if(safe[i]==true && assets[i]<min && assets[i]<limit){
    //If assets[i] to be less than the minimum then  change the minimum to the assets[i];
}


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,102
  • Joined: 19-March 11

Re: Question about a part of a code.

Posted 28 March 2014 - 11:26 PM

In this case, it's almost certainly bad code. Unless you can come up with a reason why a currency figure should be represented as a double? I can't.
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is online

  • D.I.C Addict

Reputation: 167
  • View blog
  • Posts: 788
  • Joined: 07-February 14

Re: Question about a part of a code.

Posted 28 March 2014 - 11:31 PM

Don't lose your cents :rolleyes2:/>

This post has been edited by infernorthor: 28 March 2014 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,102
  • Joined: 19-March 11

Re: Question about a part of a code.

Posted 28 March 2014 - 11:42 PM

Cents are not floating-point quantities, they're fixed-point.
Representing money as a decimal is, simply put, completely wrong. The main reason for this is that floating-point representations are inherently imprecise, without warning. If the value gets large, you lose precision, and if you are managing money, there is no circumstance where you're allowed to lose precision. A better option, though still not ideal, is to represent numeric quantities as a number of cents or of millis, depending on how finely you're tracking interest. So 1000 would represent either 1000 cents ($10) or 1000 millis ($1). This is still limited - BigInteger is better. Better still to act like you're doing object-oriented programming and use a Money class instead of having raw numbers bobbing around without context.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1