Calculer à 3 chiffres après la virgule / Calculate to 3 dec.

Just starting out? Need help? Post your questions and find answers here.
NY152
User
User
Posts: 29
Joined: Sun May 14, 2006 12:33 am

Calculer à 3 chiffres après la virgule / Calculate to 3 dec.

Post by NY152 »

Bonjour,

J'aimerais calculer un nombre qui aurait impérativement 3 chiffres après la virgule ; Exemple : 0.035 ou 0.000)

Comment s'y prendre ?

J'aimerais aussi savoir comment savoir si une variable contient un nombre négatif ?

D'avance, merci
_______________________________________________________________________________________________

Hello,

I would like to calculate a number that would have absolutely three decimal places; Example: 0.035 or 0.000)

How?

I would also like to know how to know if a variable contains a negative number?

In advance, thank you
.:NY152:.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by Dude »

NY152 wrote:I would like to calculate a number that would have absolutely three decimal places; Example: 0.035 or 0.000)
I would also like to know how to know if a variable contains a negative number?

Code: Select all

a.f=-0.005
b.f=0.040
c.f=a+b
Debug StrF(c,3)

If a<0 : Debug "'a' is negative" : EndIf
NY152
User
User
Posts: 29
Joined: Sun May 14, 2006 12:33 am

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by NY152 »

Mais si on test a < 0.xxx ca ne fonctionne pas. J'aurais aimé pouvoir le faire.
________________________________________

But if you test with a < 0.xxx it does not work. I wish I could do it.
.:NY152:.
T4r4ntul4
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by T4r4ntul4 »

Code: Select all

a.f=-0.005
If a<0.222
  Debug "'a' is negative"
EndIf
is not a problem
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by Dude »

NY152 wrote:if you test with a < 0.xxx it does not work
Yes it does. For a number to be negative, it has to be < 0. Show me your code so I can see what you're doing wrong. ;)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by netmaestro »

Code: Select all

cowbell.d = -0.123456           ; You can't limit the decimal places for the actual stored number
Debug cowbell
Debug StrD(cowbell, 3)          ; But you can control how many decimal places to display
cowbell=ValD(StrD(cowbell, 3))  ; Or truncate some if you wish
Debug cowbell
If cowbell < 0                  ; Test for negative
  Debug "NEED MORE COWBELL!!"   ; Should want more cowbell
EndIf
Last edited by netmaestro on Wed Feb 25, 2015 5:30 pm, edited 1 time in total.
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by wilbert »

netmaestro wrote:You can't limit the decimal places for the actual stored number
You could use an integer type and multiply everything by 1000 and divide by 1000 when presenting a result.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by netmaestro »

You could use an integer type and multiply everything by 1000 and divide by 1000 when presenting a result.
Sorry I was unclear. You can't limit the number of places but there's a couple of ways you can remove unwanted ones.
BERESHEIT
NY152
User
User
Posts: 29
Joined: Sun May 14, 2006 12:33 am

Re: Calculer à 3 chiffres après la virgule / Calculate to 3

Post by NY152 »

My idea is to calculate a bitrate automatically.

Is incremented by 1 as bitrate value quality 'Bit / Pixel' is not reached. I aim 0.076.

My essay:

Code: Select all

EnableExplicit
Global sLargeur.f
Global sHauteur.f
Global sIPS.f


Global Largeur.s
Global Hauteur.s
Global IPS.s

Global BitPerPixel.f

Global Bitrate.f = 100

Global pause.s

OpenConsole("Bit/Pixel Calculator")
  Print("Largeur de la vidéo : ")
  Largeur = Input()
  Print("Hauteur de la vidéo : ")
  Hauteur = Input()
  Print("Image par seconde de la vidéo : ")
  IPS = Input()
  sLargeur=ValF(Largeur)
  shauteur=ValF(Hauteur)
  sIPS=ValF(IPS)
  BitPerPixel=(sLargeur*sHauteur*sIPS)/(Bitrate*1024)
  Repeat
    If BitPerPixel < 0.076
        Bitrate=Bitrate+1
        BitPerPixel=(sLargeur*sHauteur*sIPS)/(Bitrate*1024)
        PrintN("+Bitrate: " + BitRate + " Bit/Pixel: " + BitPerPixel); For Info
      ElseIf BitPerPixel > 0.076
        Bitrate=Bitrate-1
        BitPerPixel=(sLargeur*sHauteur*sIPS)/(Bitrate*1024)
        PrintN("+Bitrate: " + BitRate + " Bit/Pixel: " + BitPerPixel)
    EndIf
  Until BitPerPixel=0.076
  Print("Bitrate: " +BitRate + " Bit/Pixel: " + BitPerPixel)
  pause=Input()
CloseConsole()
.:NY152:.
Post Reply