Post by **jeeswg** » 13 Jul 2017, 21:26

Can you give an/some example values where your script fails?

I believe that all you have to do is replace any use of Mod, with my JEE_Mod function.

Code: [Select all] [Expand]GeSHi © Codebox Plus

;[JEE_Mod function]

;Mod function returning wrong value? - AutoHotkey Community

;https://autohotkey.com/boards/viewtopic.php?f=14&t=29762&p=142063#p142063

q::

Date := 19000101000000

Loop, 50000

{

CalYear := SubStr(Date, 1, 4)

CalMonth := SubStr(Date, 5, 2)

CalDay := SubStr(Date, 7, 2)

;in this specific example you can use Floor instead of Trunc, since CalDay is positive

Num1 := ((JEE_Mod(CalDay,10)*CalMonth)*100000)+((JEE_Mod(CalYear/10,10)*10)-Floor(10*JEE_Mod(CalYear/100, 10))*10)*10000+Floor(10*JEE_Mod(CalYear/100, 10))*1000+70+(Floor(CalDay/10))

Num2 := ((Mod(CalDay,10)*CalMonth)*100000)+((Mod(CalYear/10,10)*10)-Floor(10*Mod(CalYear/100, 10))*10)*10000+Floor(10*Mod(CalYear/100, 10))*1000+70+(Floor(CalDay/10))

;original code

FirstFormulaSection := Mod(CalDay, 10) * CalMonth * 100000

SecondFormulaSection := Floor((Mod((CalYear/10),10)*10)-(Floor(10*Mod((CalYear/100),10))*10))

ThirdFormulaSection := Floor(10*Mod((CalYear/100),10))

FourthFormulaSection := Floor(CalDay/10)

SixDigitCode := Floor(FirstFormulaSection + SecondFormulaSection * 10000 + ThirdFormulaSection * 1000 + 70 + FourthFormulaSection) ;Floor() is used here to make the value a whole number with no decimals

;replaced with JEE_Mod

FirstFormulaSection := JEE_Mod(CalDay, 10) * CalMonth * 100000

SecondFormulaSection := Floor((JEE_Mod((CalYear/10),10)*10)-(Floor(10*JEE_Mod((CalYear/100),10))*10))

ThirdFormulaSection := Floor(10*JEE_Mod((CalYear/100),10))

FourthFormulaSection := Floor(CalDay/10)

SixDigitCode := Floor(FirstFormulaSection + SecondFormulaSection * 10000 + ThirdFormulaSection * 1000 + 70 + FourthFormulaSection) ;Floor() is used here to make the value a whole number with no decimals

Num1 := Round(Num1), Num2 := Round(Num2)

;if !(Num1 = Num2) || !(Num1 = SixDigitCode)

if !(Num1 = SixDigitCode)

Output .= Date "`t" Num1 "`t" Num2 "`t" SixDigitCode "`r`n"

Date += 1, Days

}

MsgBox, % Date

Clipboard := Output

MsgBox, % Output

return

I tried creating a function that works like Excel's TRUNC function (I would test this thoroughly before any industrial use), and also functions to convert between Excel and AHK style dates:

Code: [Select all] [Expand]GeSHi © Codebox Plus

JEE_Trunc(vNum, vDigits=0)

{

if !RegExMatch(vNum, "^-?\d+$|^-?\d+\.\d+$")

vNum += 0

if (vNum = "")

return ""

else if (vDigits = 0)

return Floor(Abs(vNum)) * (vNum >= 0) ? 1 : -1

else if (vDigits < 0)

return Floor(Abs(vNum)/(10**Abs(vDigits))) * (10**Abs(vDigits)) * (vNum >= 0) ? 1 : -1

else if (vDigits > 0)

{

vNum := RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)|\.0+$|\..*?\K0+$") ;remove leading/trailing zeros

return RegExReplace(vNum, ".*\.\d{" vDigits "}\K.*", "") ;crop

}

}

;==================================================

JEE_DateExcelToAhk(vNum, vFormat = "yyyyMMddHHmmss")

{

vDate := 18991230

vDate += vNum * 86400, Seconds

FormatTime, vDate, % vDate, % vFormat

return vDate

}

;==================================================

JEE_DateAhkToExcel(vNum)

{

vDate -= 18991230, Seconds

return vDate / 86400

}

[EDIT:] Here's some code to truncate to an integer, which is another way of doing what is done in Helgef's trunc function.

Can you give an/some example values where your script fails?

I believe that all you have to do is replace any use of Mod, with my JEE_Mod function.

[code]

;[JEE_Mod function]

;Mod function returning wrong value? - AutoHotkey Community

;https://autohotkey.com/boards/viewtopic.php?f=14&t=29762&p=142063#p142063

q::

Date := 19000101000000

Loop, 50000

{

CalYear := SubStr(Date, 1, 4)

CalMonth := SubStr(Date, 5, 2)

CalDay := SubStr(Date, 7, 2)

;in this specific example you can use Floor instead of Trunc, since CalDay is positive

Num1 := ((JEE_Mod(CalDay,10)*CalMonth)*100000)+((JEE_Mod(CalYear/10,10)*10)-Floor(10*JEE_Mod(CalYear/100, 10))*10)*10000+Floor(10*JEE_Mod(CalYear/100, 10))*1000+70+(Floor(CalDay/10))

Num2 := ((Mod(CalDay,10)*CalMonth)*100000)+((Mod(CalYear/10,10)*10)-Floor(10*Mod(CalYear/100, 10))*10)*10000+Floor(10*Mod(CalYear/100, 10))*1000+70+(Floor(CalDay/10))

;original code

FirstFormulaSection := Mod(CalDay, 10) * CalMonth * 100000

SecondFormulaSection := Floor((Mod((CalYear/10),10)*10)-(Floor(10*Mod((CalYear/100),10))*10))

ThirdFormulaSection := Floor(10*Mod((CalYear/100),10))

FourthFormulaSection := Floor(CalDay/10)

SixDigitCode := Floor(FirstFormulaSection + SecondFormulaSection * 10000 + ThirdFormulaSection * 1000 + 70 + FourthFormulaSection) ;Floor() is used here to make the value a whole number with no decimals

;replaced with JEE_Mod

FirstFormulaSection := JEE_Mod(CalDay, 10) * CalMonth * 100000

SecondFormulaSection := Floor((JEE_Mod((CalYear/10),10)*10)-(Floor(10*JEE_Mod((CalYear/100),10))*10))

ThirdFormulaSection := Floor(10*JEE_Mod((CalYear/100),10))

FourthFormulaSection := Floor(CalDay/10)

SixDigitCode := Floor(FirstFormulaSection + SecondFormulaSection * 10000 + ThirdFormulaSection * 1000 + 70 + FourthFormulaSection) ;Floor() is used here to make the value a whole number with no decimals

Num1 := Round(Num1), Num2 := Round(Num2)

;if !(Num1 = Num2) || !(Num1 = SixDigitCode)

if !(Num1 = SixDigitCode)

Output .= Date "`t" Num1 "`t" Num2 "`t" SixDigitCode "`r`n"

Date += 1, Days

}

MsgBox, % Date

Clipboard := Output

MsgBox, % Output

return

[/code]

I tried creating a function that works like Excel's TRUNC function (I would test this thoroughly before any industrial use), and also functions to convert between Excel and AHK style dates:

[code]

JEE_Trunc(vNum, vDigits=0)

{

if !RegExMatch(vNum, "^-?\d+$|^-?\d+\.\d+$")

vNum += 0

if (vNum = "")

return ""

else if (vDigits = 0)

return Floor(Abs(vNum)) * (vNum >= 0) ? 1 : -1

else if (vDigits < 0)

return Floor(Abs(vNum)/(10**Abs(vDigits))) * (10**Abs(vDigits)) * (vNum >= 0) ? 1 : -1

else if (vDigits > 0)

{

vNum := RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)|\.0+$|\..*?\K0+$") ;remove leading/trailing zeros

return RegExReplace(vNum, ".*\.\d{" vDigits "}\K.*", "") ;crop

}

}

;==================================================

JEE_DateExcelToAhk(vNum, vFormat = "yyyyMMddHHmmss")

{

vDate := 18991230

vDate += vNum * 86400, Seconds

FormatTime, vDate, % vDate, % vFormat

return vDate

}

;==================================================

JEE_DateAhkToExcel(vNum)

{

vDate -= 18991230, Seconds

return vDate / 86400

}

[/code]

[EDIT:] Here's some code to truncate to an integer, which is another way of doing what is done in Helgef's trunc function.

[code]

q:: ;truncate to an integer (i.e. round to the nearest integer towards 0)

vNum := 123.456

MsgBox, % vNum := (vNum >= 0) ? Floor(vNum) : Ceil(vNum)

vNum := -123.456

MsgBox, % vNum := (vNum >= 0) ? Floor(vNum) : Ceil(vNum)

return

[/code]