Using GNU Octave the calculations are approximate:
octave:1> x = 10^40
x = 1.0000e+40
octave:2> y = 10^12
y = 1.0000e+12
octave:3> (1-1/x)^y
ans = 1
octave:8> exp(-(y/x + y /(2*x*x)))
ans = 1
Provided that the calculation made by Daniel Fischer is correct, the code to calculate exp(-(Y/X+ Y/(2*X*X)))
in Java using BigDecimal is:
public static void main(String[] args) {
BigDecimal x = new BigDecimal(10,MathContext.UNLIMITED).pow(40);
BigDecimal y = new BigDecimal(10,MathContext.UNLIMITED).pow(12);
BigDecimal twoXSquared = new BigDecimal(2,MathContext.UNLIMITED).multiply(x).multiply(x);
BigDecimal yDividedByTwoXSquared = y.divide(twoXSquared);
BigDecimal yDividedByX = y.divide(x);
BigDecimal exponent = new BigDecimal(-1,MathContext.UNLIMITED).multiply(yDividedByX.add(yDividedByTwoXSquared));
System.out.println(exponent.toEngineeringString());
BigDecimal result = new BigDecimal(Math.E,MathContext.UNLIMITED).pow(exponent.intValue());
System.out.println(result.toEngineeringString());
}