Jump to content

Litle problem with a number rounded...


 Share

Recommended Posts

I want calculate the time in hour, minutes and seconds,but in some cases the rounding isn't good.

$Distance = 1000
$Heures = 1
$Minutes = 60
$Secondes = 60
; Calcul du temps en secondes :
$Temps = (($Heures*60)*60)+($Minutes*60)+$Secondes; Calcul de la vitesse en km/h :
$Vitesse = Round((($Distance/$Temps) * 3.6), 1)
; Calcul du temps au km : Temps = Distance / vitesse
; Calcul du temps au km : Temps = Distance / vitesse
$Temps_km = (1000 * $Temps)/$Distance ; Temps en secondes
$Temps_km_h = Floor($Temps_km/3600) ; OK
$Temps_km_m = Floor(Mod(($Temps_km/3600), 1) * 60) ; OK
$Temps_km_s = Round(Mod(Mod(($Temps_km/3600), 1) * 60, 1) * 60)
ConsoleWrite("Temps ensecondes : " & $Temps & @CRLF & "Vitesse en km/h : " & $Vitesse & @CRLF & "Temps au km : " & $Temps_km_h & " h " & $Temps_km_m & " m " &$Temps_km_s & " s" & @CRLF)

The result is Temps au km : 2h 0m 60s and I want 2h 1m :-)

Link to comment
Share on other sites

I want calculate the time in hour, minutes and seconds,but in some cases the rounding isn't good.

It's the whole binary representation of decimal numbers problem again. You're going to need to do some checking after your math and before your output.

If s=60, then m=m+1 and s=0

If m=60, then h=h+1 and m=0

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

This is a simple fix except for it lacking a way to set $Temp_km_s to the remainder.

If $Temps_km_s >= 60 Then
   $Temps_km_m += $Temps_km_s/60
   $Temps_km_s = 0
EndIf

Your script seems to calculate the minutes effectively unless the amount of remaining seconds are exactly 60.

I hope this helps.

Spoiler

censored.jpg

 

Link to comment
Share on other sites

If $Temps_km_s >= 60 Then
   $Temps_km_m += $Temps_km_s/60
   $Temps_km_s = 0
EndIf

You forgot to check if adjusting $Temps_km_m will cause it to reach 60 and thus needing to adjust $Temps_km_h.

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

I wanted to see if there was a way to get the remainder so here is another example.

If $Temps_km_s >= 60 Then
   $Temps_km_m += $Temps_km_s/60
   $Remainder = StringSplit(Round($Temps_km_s/60, 1), ".")
   $Temps_km_s = $Remainder[2]
   If $Temps_km_m >= 60 Then
   $Temps_km_h += $Temps_km_m/60
   $Remainder = StringSplit(Round($Temps_km_m/60, 1), ".")
   $Temps_km_m = $Remainder[2]
   EndIf
EndIf
Spoiler

censored.jpg

 

Link to comment
Share on other sites

Try this, you're all over complicating things:

$Distance = 1000
$Heures = 1
$Minutes = 60
$Secondes = 65
; Calcul du temps en secondes :
$Temps = (($Heures * 3600) + ($Minutes * 60) + $Secondes); Calcul de la vitesse en km/h :
$Vitesse = Round((($Distance / $Temps) * 3.6), 1)
; Calcul du temps au km : Temps = Distance / vitesse
; Calcul du temps au km : Temps = Distance / vitesse
$Temps_km = (1000 * $Temps) / $Distance ; Temps en secondes
$Temps_km_h = Int($Temps / 3600) ; OK
$Temps_km_m = Int(($Temps - ($Temps_km_h * 3600)) / 60)
$Temps_km_s = Int(($Temps - ($Temps_km_h * 3600) - ($Temps_km_m * 60)))
ConsoleWrite("Temps ensecondes : " & $Temps & @CRLF & "Vitesse en km/h : " & $Vitesse & @CRLF & "Temps au km : " & $Temps_km_h & " h " & $Temps_km_m & " m " &$Temps_km_s & " s" & @CRLF)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Try this, you're all over complicating things:

Perhaps, but at least we didn't change the original problem.

$Temps_km_h = Int($Temps / 3600)   ;  does not equal the original ...  $Temps_km_h = Floor($Temps_km/3600) ; OK

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

The original problem was that it wasn't working, this works as intended. What's the issue you're seeing?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

OP is dividing $Temps_km by 3600.

You are dividing $Temps by 3600.

$Temps_km in OP's equation is previously also divided by $Distance.

Two different results.

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

I'd just add a work variable and simplify life even more:

$Temps_km = (1000 * $Temps)/$Distance ; Temps en secondes
$Temps_km_h = Int($Temps_km/3600)
Local $tmp_Temps = $Temps_km - $Temps_km_h * 3600
$Temps_km_m = Int($tmp_Temps/ 60)
$Temps_km_s = $tmp_Temps - $Temps_km_m * 60
Link to comment
Share on other sites

OP is dividing $Temps_km by 3600.

You are dividing $Temps by 3600.

$Temps_km in OP's equation is previously also divided by $Distance.

Two different results.

I see what you're saying now, simple fix. Change every instance of $Temps in the calculations to $Temps_km and it works out correctly. It just so happened that the distance figure he used in his example made the calculations the same for either variable.

Here's the updated script using the correct figures.

$Distance = 10000
$Heures = 1
$Minutes = 60
$Secondes = 65
; Calcul du temps en secondes :
$Temps = (($Heures * 3600) + ($Minutes * 60) + $Secondes); Calcul de la vitesse en km/h :
$Vitesse = Round((($Distance / $Temps) * 3.6), 1)
; Calcul du temps au km : Temps = Distance / vitesse
; Calcul du temps au km : Temps = Distance / vitesse
$Temps_km = (1000 * $Temps) / $Distance ; Temps en secondes
$Temps_km_h = Int($Temps_km / 3600) ; OK
$Temps_km_m = Int(($Temps_km - ($Temps_km_h * 3600)) / 60)
$Temps_km_s = Int(($Temps_km - ($Temps_km_h * 3600) - ($Temps_km_m * 60)))
ConsoleWrite("Temps ensecondes : " & $Temps & @CRLF & "Vitesse en km/h : " & $Vitesse & @CRLF & "Temps au km : " & $Temps_km_h & " h " & $Temps_km_m & " m " &$Temps_km_s & " s" & @CRLF)

Also, yes it would be much simpler to read it if you use variables to hold the intermediate calculations, but the results are the same.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I see what you're saying now, simple fix. Change every instance of $Temps in the calculations to $Temps_km and it works out correctly. It just so happened that the distance figure he used in his example made the calculations the same for either variable.

Here's the updated script using the correct figures.

$Distance = 10000
$Heures = 1
$Minutes = 60
$Secondes = 65
; Calcul du temps en secondes :
$Temps = (($Heures * 3600) + ($Minutes * 60) + $Secondes); Calcul de la vitesse en km/h :
$Vitesse = Round((($Distance / $Temps) * 3.6), 1)
; Calcul du temps au km : Temps = Distance / vitesse
; Calcul du temps au km : Temps = Distance / vitesse
$Temps_km = (1000 * $Temps) / $Distance ; Temps en secondes
$Temps_km_h = Int($Temps_km / 3600) ; OK
$Temps_km_m = Int(($Temps_km - ($Temps_km_h * 3600)) / 60)
$Temps_km_s = Int(($Temps_km - ($Temps_km_h * 3600) - ($Temps_km_m * 60)))
ConsoleWrite("Temps ensecondes : " & $Temps & @CRLF & "Vitesse en km/h : " & $Vitesse & @CRLF & "Temps au km : " & $Temps_km_h & " h " & $Temps_km_m & " m " &$Temps_km_s & " s" & @CRLF)

Also, yes it would be much simpler to read it if you use variables to hold the intermediate calculations, but the results are the same.

Thanks a lot BrewManNH !!!!

You are a genius man ! You have the good sense ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...