I have a float variable which describes a term in months.
I need to be able to add this period to a given date.
I created a simple logic such as:
DECLARE #monthsTerm AS float = 2;
Select DATEADD(dd,((CAST(#monthsTerm AS INT) % 1) / 0.25 * 7), (DATEADD(mm, CAST(#monthsTerm AS INT), '2018-01-01'))) AS [Subscription end date]
The problem appears when I want to set the monthsTerm variable to a 1, 2 or 3 weeks, etc. period of time. So for two weeks period, the variable would look like:
DECLARE #monthsTerm AS float = 0.5;
and I expect to get '2018-01-15' as a result, but now it returns '2018-01-01' which is wrong.
I thought about transferring the expression to just a:
Select DATEADD(dd,((#monthsTerm % 1) / 0.25 * 7), (DATEADD(mm, CAST(#monthsTerm AS INT), '2018-01-01'))) AS [Subscription end date]
however, I'm getting an exception:
The data types float and int is incompatible in the modulo operator.
I know that for instance the similar expression would work in C#:
public DateTime EndDate
=> StartDate.AddMonths((int)TermMonths).AddDays((TermMonths % 1) / 0.25 * 7);
Is there any simple workaround to this so I can have only one simple expression with Select DATEADD(.. or I need to end up with some more complex solution? Cheers
Select
cast((CAST(#monthsTerm*100 as int) % 100)*7 /0.25/100 as int) as days ,
(((CAST(#monthsTerm*100 as int) % 100)/100) / 0.25 * 7),
DATEADD(dd,cast((CAST(#monthsTerm*100 as int) % 100)*7 /0.25/100 as int), (DATEADD(mm, CAST(#monthsTerm AS INT), '2018-01-01'))) AS [Subscription end date]
Multiplying with 100, and dividing by 100 after the %
How would you implement the equivalent to Java's unsigned right shift operator in Swift?
According to Java's documentation, the unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
So, for instance,
long s1 = (-7L >>> 16); // result is 281474976710655L
long s2 = (-7L >> 16); // result is -1
In order to implement this in Swift, I would take all the bits except the sign bit by doing something like,
let lsb = Int64.max + negativeNumber + 1
Notice that the number has to be negative! If you overflow the shift operator, the app crashes with EXC_BAD_INSTRUCTION, which it's not very nice...
Also, I'm using Int64 on purpose. Because there's no bigger datatype, doing something like (1 << 63) would overflow the Int64 and also crash. So instead of doing ((1 << 63) - 1 + negativeNumber) in a bigger datatype, I wrote it as Int64.max + negativeNumber - 1.
Then, shift that positive number with the normal logical shift, and OR the bit from the sign in the first left bit after the sign.
let shifted = (lsb >> bits) | 0x4000000000000000
However, that doesn't give me the expected result,
((Int64.max - 7 + 1) >> 16) | 0x4000000000000000 // = 4611826755915743231
Not sure what I'm doing wrong...
Also, would it be possible to name this operator '>>>' and extend Int64?
Edit:
Adding here the solution from OOper below,
infix operator >>> : BitwiseShiftPrecedence
func >>> (lhs: Int64, rhs: Int64) -> Int64 {
return Int64(bitPattern: UInt64(bitPattern: lhs) >> UInt64(rhs))
}
I was implementing the Java Random class in Swift, which also involves truncating 64-bit ints into 32-bit. Thanks to OOper I just realized I can use the truncatingBitPattern initializer to avoid overflow exceptions. The function 'next' as described here becomes this in Swift,
var seed: Int64 = 0
private func next(_ bits: Int32) -> Int32 {
seed = (seed &* 0x5DEECE66D &+ 0xB) & ((1 << 48) - 1)
let shifted : Int64 = seed >>> (48 - Int64(bits))
return Int32(truncatingBitPattern: shifted)
}
One sure way to do it is using the unsigned shift operation of unsigned integer type:
infix operator >>> : BitwiseShiftPrecedence
func >>> (lhs: Int64, rhs: Int64) -> Int64 {
return Int64(bitPattern: UInt64(bitPattern: lhs) >> UInt64(rhs))
}
print(-7 >>> 16) //->281474976710655
(Using -7 for testing with bit count 16 does not seem to be a good example, it loses all significant bits with 16-bit right shift.)
If you want to do it in your way, the bitwise-ORed missing sign bit cannot be a constant 0x4000000000000000. It needs to be 0x8000_0000_0000_0000 (this constant overflows in Swift Int64) when bit count == 0, and needs to be logically shifted with the same bits.
So, you need to write something like this:
infix operator >>>> : BitwiseShiftPrecedence
func >>>> (lhs: Int64, rhs: Int64) -> Int64 {
if lhs >= 0 {
return lhs >> rhs
} else {
return (Int64.max + lhs + 1) >> rhs | (1 << (63-rhs))
}
}
print(-7 >>>> 16) //->281474976710655
It seems far easier to work with unsigned integer types when you need unsigned shift operation.
Swift has unsigned integer types, so there is no need to a separate unsigned right shift operator. That's a choice in Java that followed from the decision to not have unsigned types.
There is a script that makes the effect when I click mouse buttons.
Code of this script.
#NoEnv
CoordMode Mouse, Screen
Setup()
~LButton::ShowRipple(LeftClickRippleColor)
~MButton::ShowRipple(MiddleClickRippleColor)
~RButton::ShowRipple(RightClickRippleColor)
Setup()
{
Global
RippleWinSize := 170
RippleStep := 4
RippleMinSize := 10
RippleMaxSize := RippleWinSize - 20
RippleAlphaMax := 0x4147
RippleAlphaStep := RippleAlphaMax // ((RippleMaxSize - RippleMinSize) / RippleStep)
RippleVisible := False
LeftClickRippleColor := 0xff0000
MiddleClickRippleColor := 0xff00ff
RightClickRippleColor := 0xffa500
DllCall("LoadLibrary", Str, "gdiplus.dll")
VarSetCapacity(buf, 16, 0)
NumPut(1, buf)
DllCall("gdiplus\GdiplusStartup", UIntP, pToken, UInt, &buf, UInt, 0)
Gui Ripple: -Caption +LastFound +AlwaysOnTop +ToolWindow +Owner +E0x80000
Gui Ripple: Show, NA, RippleWin
hRippleWin := WinExist("RippleWin")
hRippleDC := DllCall("GetDC", UInt, 0)
VarSetCapacity(buf, 40, 0)
NumPut(40, buf, 0)
NumPut(RippleWinSize, buf, 4)
NumPut(RippleWinSize, buf, 8)
NumPut(1, buf, 12, "ushort")
NumPut(32, buf, 14, "ushort")
NumPut(0, buf, 16)
hRippleBmp := DllCall("CreateDIBSection", UInt, hRippleDC, UInt, &buf, UInt, 0, UIntP, ppvBits, UInt, 0, UInt, 0)
DllCall("ReleaseDC", UInt, 0, UInt, hRippleDC)
hRippleDC := DllCall("CreateCompatibleDC", UInt, 0)
DllCall("SelectObject", UInt, hRippleDC, UInt, hRippleBmp)
DllCall("gdiplus\GdipCreateFromHDC", UInt, hRippleDC, UIntP, pRippleGraphics)
DllCall("gdiplus\GdipSetSmoothingMode", UInt, pRippleGraphics, Int, 4)
MouseGetPos _lastX, _lastY
SetTimer MouseIdleTimer, 5000
Return
MouseIdleTimer:
MouseGetPos _x, _y
if (_x == _lastX and _y == _lastY)
ShowRipple(MouseIdleRippleColor, _interval:=20)
else
_lastX := _x, _lastY := _y
Return
}
ShowRipple(_color, _interval:=10)
{
Global
if (RippleVisible)
Return
RippleColor := _color
RippleDiameter := RippleMinSize
RippleAlpha := RippleAlphaMax
RippleVisible := True
MouseGetPos _pointerX, _pointerY
SetTimer RippleTimer, % _interval
Return
RippleTimer:
DllCall("gdiplus\GdipGraphicsClear", UInt, pRippleGraphics, Int, 0)
if ((RippleDiameter += RippleStep) < RippleMaxSize) {
DllCall("gdiplus\GdipCreatePen1", Int, ((RippleAlpha -= RippleAlphaStep) << 24) | RippleColor, float, 3, Int, 2, UIntP, pRipplePen)
DllCall("gdiplus\GdipDrawEllipse", UInt, pRippleGraphics, UInt, pRipplePen, float, 1, float, 1, float, RippleDiameter - 1, float, RippleDiameter - 1)
DllCall("gdiplus\GdipDeletePen", UInt, pRipplePen)
}
else {
RippleVisible := False
SetTimer RippleTimer, Off
}
VarSetCapacity(buf, 8)
NumPut(_pointerX - RippleDiameter // 2, buf, 0)
NumPut(_pointerY - RippleDiameter // 2, buf, 4)
DllCall("UpdateLayeredWindow", UInt, hRippleWin, UInt, 0, UInt, &buf, Int64p, (RippleDiameter + 5) | (RippleDiameter + 5) << 32, UInt, hRippleDC, Int64p, 0, UInt, 0, UIntP, 0x1FF0000, UInt, 2)
Return
}
How this script to work:
I often do double click left mouse button. I want to have the same effect on my gif was and when I double click left mouse button.
Could anyone of you tell what needs to be done to effect other color, when I make double click left mouse button?
Thanks.
First variant:
~LButton::
if(A_PriorHotkey = "~LButton" && A_TimeSincePriorHotkey < 200){
RippleVisible := False
ShowRipple(0x2E0854)
} else {
ShowRipple(LeftClickRippleColor)
}
return
When I click left mouse button shows the color set for left mouse button, but double-click to change the color to the color for double-click.
Second variant:
DoubleClickWait := 200
~LButton::
SetTimer, SingleClick, Off
if(A_PriorHotkey = "~LButton" && A_TimeSincePriorHotkey < DoubleClickWait){
ShowRipple(0x2E0854)
} else {
SetTimer, SingleClick, -%DoubleClickWait%
}
return
SingleClick:
ShowRipple(LeftClickRippleColor)
return
Ripple effect are make after DelayTime (200 ms in example). If within 200 ms after the first mouse click will not be second click will show the color for a single click. If 200 ms is committed another click, will show the color for the double-click.
Many thanks Capn Odin AutoHotkey user.
I understand that arc4random returns an unsigned integer up to (2^32)-1. In this scenario it it always gives a number between 0 and 1.
var x:UInt32 = (arc4random() / 0xFFFFFFFF)
How does the division by 0xFFFFFFFF cause the number to be between 0 - 1?
As you've stated,
arc4random returns an unsigned integer up to (2^32)-1
0xFFFFFFFF is equal to (2^32)-1, which is the largest possible value of arc4random(). So the arithmetic expression (arc4random() / 0xFFFFFFFF) gives you a ratio that is always between 0 and 1 — and as this is an integer division, the result can only be between 0 and 1.
to receive value between 0 and 1, the result must be floating point value
import Foundation
(1..<10).forEach { _ in
let x: Double = (Double(arc4random()) / 0xFFFFFFFF)
print(x)
}
/*
0.909680047749933
0.539794033984606
0.049406117305487
0.644912529188421
0.00758233550181201
0.0036165844657497
0.504160538898818
0.879743074271768
0.980051155663107
*/
I'm trying to toggle mouse trails on and off with a keypress (windows key + q) using an autohotkey script, but can't get it working.
This is my code-
#q::
if DllCall("SystemParametersInfo", 94) < 2
{
DllCall("SystemParametersInfo", UInt, 0x005E, UInt, 0, UInt, 9, UInt, 0)
}else {
DllCall("SystemParametersInfo", UInt, 0x005E, UInt, 0, UInt, 0, UInt, 0)
}
return
Are you able to help?
First read the value using SPI_GETMOUSETRAILS into a variable by reference (intP) then set the new value using SPI_SETMOUSETRAILS:
#q::
DllCall("SystemParametersInfo", int,SPI_GETMOUSETRAILS:=0x5E, int,0, intP,length, int,0)
length := length > 1 ? 0 : 9
DllCall("SystemParametersInfo", int,SPI_SETMOUSETRAILS:=0x5D, int,length, int,0, int,0)
return